We need to solve: Given an array A[1..N], each A_i is number of cards with integer i. We want to form as many pairs as possible, each pair consists of two cards, and the absolute difference of their numbers is at most 1. So a pair can be (i,i) or (i,i+1). No card can be used in more than one pair.
Goal: maximize number of pairs.
Constraints: N up to 1e5, A_i up to 1e9.
We need to compute max number of pairs.
This is reminiscent of a matching problem on a line: we have groups of nodes (cards) at positions 1..N, and we can match within same position or adjacent positions.
The total number of cards is sum A_i, and we want max pairs, i.e., floor(some matching). This is essentially maximizing the number of matched cards.
We can think of it as: each pair uses two cards, and the allowed pairs are edges between same node or adjacent nodes. It's a graph that is a path where each node has A_i self-loop? Actually, we can treat each card as a unit of capacity at node i. We can match within node i: up to floor(A_i/2) pairs. Also we can match between node i and i+1: up to min(A_i, A_{i+1})? But careful: if we match within nodes and across nodes, there is interaction: using cards for cross matches reduces cards available for self-matches.
So we need to decide allocation to maximize total pairs.
This is a kind of maximum matching in a bipartite graph? But cards are identical except for their integer. The problem can be solved greedily? Or via DP? Since constraints large, we need O(N) or O(N log N) algorithm.
Observation: Each pair consumes two cards. The maximum possible pairs is floor(total_cards/2). But constraints on adjacency may limit.
We need to maximize number of pairs. Equivalent to: we want to minimize number of unmatched cards (or maximize matched cards). Since pairs are undirected, we can think of forming matches as we traverse.
Simplify: think of it as: we have piles of cards. We can combine cards from pile i with i or i+1. This is reminiscent of "pair removal" problem.
We can treat each card as a unit. We can process from left to right, deciding how many cross pairs to form between i and i+1, while leaving as many cards for self-pairs and future cross pairs.
One approach: Greedy from left to right: at each i, first pair within i as much as possible? But cross pairs might be more beneficial? Actually, cross pairs also consume two cards, but can enable more pairs? For example, if A_i is odd, and A_{i+1} is large, pairing the leftover single card from i with one from i+1 may be better than leaving it unmatched. So it seems optimal to match as many as possible, but we need to decide cross matching.
Consider DP with state representing how many cards from current pile are left after internal pairing and cross pairing with previous pile? Since matching only with i-1 and i+1, the only dependence is on leftover from previous pile. Let's define after processing piles up to i-1, we may have some leftover cards from pile i-1 that were not paired with i-1 itself (because odd count) and also not paired with i-2 (since we process left to right, pairing i-1 with i may be pending). Actually, when processing i, we can decide to pair some cards from i-1 with i. But we already processed i-1? To incorporate this, we can process sequentially and maintain how many cards from previous pile are available to pair with current pile.
Formally, let x be the number of cards from pile i-1 that are not yet paired (i.e., leftover after internal pairing and pairing with i-2). Then we can pair some of these leftover with cards from pile i. Also we can pair within pile i. After that, we may have leftover from pile i to pass to next pile.
We need to decide how many pairs to make between i-1 and i to maximize total pairs. Since each pair yields 1, and the total number of pairs is sum of internal pairs plus cross pairs.
We can devise a DP: For i from 1 to N, we have A_i. Let dp[i][r] be max pairs from first i piles, with r leftover cards from pile i (i.e., not matched with i-1 or internally). r can be 0,1,2? Actually, because we can pair within pile i, leftover can be at most 1? Let's think: We can pair cards within pile i in groups of 2. If we have an odd number, one can be leftover. If we also have leftover from previous pile, we might pair that one with one from this pile, possibly reducing leftover. But if we have leftover from previous pile, we should pair it with one from this pile if possible, because leaving it unmatched reduces total pairs. So we always want to pair leftover from previous pile with a card from current pile if possible, unless we want to save current cards for something else? But pairing with previous leftover gives a pair; if we don't pair it, that leftover remains unmatched eventually (since we only have future piles i+1 that can pair with it? Actually, leftover from pile i-1 can only be paired with i (since difference at most 1, i-2 is also adjacent? Wait: leftover from pile i-1 is after pairing with i-2 and internally. It can still be paired with i. That's the only option. If we don't pair it with i, it becomes permanently unmatched (since we go forward and we won't revisit i-1). So it's always optimal to pair leftover from previous pile with current pile if current pile has any card available. The only nuance: If we have more than one leftover? But leftover can be at most 1? Let's analyze: Could leftover from a pile be more than 1? Suppose we have A_i cards. We can pair them internally: floor(A_i/2) pairs. After internal pairing, we have A_i % 2 leftover (0 or 1). Then we can also pair with previous pile's leftover. But we could also choose not to pair some cards internally to use them for cross pairing? Could it be beneficial to leave some cards unpaired internally to increase cross pairing? Cross pairing also gives one pair per two cards. So both internal and cross pairs yield one pair per two cards. The difference is that cross pairs use cards from two piles, while internal uses same pile. If we have an even number of cards in a pile, we could either pair all internally, or leave some to pair with neighbors. But consider: two piles i and i+1. If we have A_i even, we could pair all internally, leaving 0; then cross pair 0. Alternatively, we could leave some unpaired to cross with i+1. But that would reduce internal pairs by 1 (since we use two cards that would have been internally paired, now one of them might cross pair? Actually, to cross pair, we need one card from i and one from i+1, which yields one pair. If we instead pair both internally: two cards from i -> one internal pair; and two cards from i+1 internally -> one internal pair; total 2 pairs. If we cross pair one from i with one from i+1, then we have one card left in i (if originally even 2, after using 1 cross, left 1) and one left in i+1? Actually, suppose A_i=2, A_{i+1}=2. Option 1: internal pairs: pair i internally (1 pair) and i+1 internally (1 pair) total 2. Option 2: cross pair 1 from i with 1 from i+1 (1 pair), then remaining 1 in i and 1 in i+1 can be paired internally? They are odd leftovers can't be paired internally alone (need 2). Could they be paired cross? No, because after using one cross, we have leftover 1 each, which could be paired cross? But cross pairing also requires one from each pile, so we could pair them together: that would be another cross pair, using the remaining 1 from i and 1 from i+1, giving 2 cross pairs total. That yields 2 pairs as well. So same.
What about A_i=2, A_{i+1}=1: internal pairs: i gives 1, i+1 gives 0 (since odd, but could be paired cross with leftover from i? i leftover after internal: if internal pair both? Actually if we pair both internally, i uses both, leftover 0; i+1 has 1, unmatched. total 1. If we instead cross pair: use 1 from i with 1 from i+1 => 1 pair. Then i leftover 1 (cannot pair internally) => unmatched. total 1. Same. Could we do better by not using internal? If we don't pair i internally, we have 2 cards at i. Cross pair one with i+1 => 1 pair; left one at i unmatched. That's 1 again. So equal.
But there might be cases where we prefer to keep a card for cross with next next? No, because cross only allowed with difference <=1, so i can pair with i+1, not i+2. So leftover from i can only pair with i+1. So it's only about the immediate neighbor.
Thus it appears that the optimal strategy is: process from left to right, maintain leftover from previous pile, and at current pile, we try to pair that leftover with a card from current pile if possible. Then we can pair as many as possible within current pile (internal pairs) using the remaining cards (since leftover from previous has been already paired). After internal pairing, we will have either 0 or 1 leftover from current pile (if odd count). That leftover will be passed to next pile. This greedy seems optimal? Let's test on some examples.
Example 1: N=4, A=[4,0,3,2]. Process:
i=1: no previous leftover. Internal pairs: floor(4/2)=2, leftover = 0? Actually 4 even -> 0 leftover. So pairs=2, leftover=0.
i=2: A2=0, previous leftover=0. Internal pairs=0, leftover=0.
i=3: A3=3, previous leftover=0. Internal pairs = floor(3/2)=1, leftover=1 (since odd). pairs+=1 => total 3.
i=4: A4=2, previous leftover=1. First pair the leftover with one from i4: use 1 from A4 to pair with leftover, that gives 1 cross pair. After that, A4 left = 2-1=1. Then internal pairs: floor(1/2)=0, leftover=1? Actually after using 1, we have 1 card left. Internal pairs=0, leftover=1 (from i4). But we could also consider maybe we shouldn't pair the leftover with i4 if that prevents internal pair? Let's see: if we don't pair the leftover, then leftover from i3 (1) remains unmatched. At i4 we have 2 cards: internal pair => 1 pair, leftover 0. So total pairs would be previous 3 +1 =4, and leftover i3 unmatched (but doesn't count). That also gives 4. So both yield 4. But our greedy gave: cross pair 1 (from i3 with i4) and then internal none, leftover from i4 =1 (unmatched later? There is no i5, so that leftover goes unmatched). So pairs total = 2+0+1+1 =4. So same.
But does greedy ever fail? Consider a scenario where we have a small number in the middle that could be better used to pair with both sides? Actually a card can only be used once. So if we have A_i = 1, and both A_{i-1} and A_{i+1} are large, we might need to decide whether to pair i's card with left or right. Greedy left-to-right would pair with left leftover (if any) before considering internal pairing at i. That means leftover from i-1 will consume i's card if available, potentially leaving i with no card for internal or for pairing with i+1. But maybe it's better to not pair with left, to keep i's card to pair with right, if that yields more total pairs? Let's test: A_{i-1}=1, A_i=1, A_{i+1}=1. Greedy: i-1: internal 0, leftover 1. i: previous leftover=1, A_i=1 => pair them (cross pair) => 1 pair, A_i becomes 0, leftover=0. Then i+1: previous leftover=0, A_{i+1}=1 => internal 0, leftover=1 -> unmatched. Total pairs=1. Alternative: At i-1: internal 0 leftover 1. At i: decide not to pair with leftover; then leftover from i-1 stays unmatched? Actually if we don't pair, then i has 1 card; we could pair i internally? cannot; leftover from i-1 remains. Then we could pair i with i+1: we have i=1, i+1=1 => cross pair => 1 pair, and leftover from i-1 unmatched. Total pairs also 1. So same.
What about A_{i-1}=2, A_i=1, A_{i+1}=2? Greedy: i-1: internal pairs = floor(2/2)=1, leftover 0. (since even, no leftover). Wait A_{i-1}=2 even => internal pair 1, leftover 0. So no leftover passed. Then i: previous leftover=0, A_i=1 -> internal 0, leftover 1. i+1: previous leftover=1, A_{i+1}=2 => pair leftover with one from i+1 => cross pair 1, then A_{i+1} left =1, internal 0, leftover 1. Total pairs =1 (i-1 internal) +0 +1 =2? Actually i-1 gave 1, cross pair gave 1, total 2. Alternatively, maybe we could have arranged differently: At i-1, we could choose not to pair internally, leaving 2 cards. Then we could pair one of them with i (cross) and then the remaining one at i-1? But then what? Let's simulate: i-1=2. If we don't pair internally, leftover 0? Actually if we don't pair, we can treat as we have 2 unpaired cards. But they can be used for cross with i. But we need to decide for maximum pairs. So we could pair one from i-1 with i (1 cross). Then left: i-1:1 card; i:0 (since used); i+1:2. Now we could pair the remaining i-1 card with? i-1 can only pair with i (already used) or i-2 (none). So it's unmatched. Then at i+1, we have 2, internal pair =1. Total =2 (1 cross +1 internal). Same.
What if we pair both from i-1 with i? Not possible because i has only 1. So seems greedy yields 2.
Consider A_{i-1}=3, A_i=1, A_{i+1}=3. Greedy: i-1: floor(3/2)=1 internal, leftover 1. i: previous leftover=1, A_i=1 => cross pair, A_i becomes 0, leftover=0, pairs+1 (cross). i+1: previous leftover=0, A_{i+1}=3 => internal floor(3/2)=1, leftover 1. Total pairs =1 (i-1 internal) +1 (cross) +1 (i+1 internal) =3. Alternative: at i-1, we could choose not to pair internally, leaving 3 cards. Then pair one with i: cross =>1. Left i-1=2, i=0, i+1=3. Now i-1 has 2, could pair internally =>1. i+1=3 can pair internally =>1, leftover 1. Total =1+1+1=3. Same.
What about scenario where greedy might pair leftover from previous with current, but maybe it's better to save current's cards for internal pairs if current is even? But pairing a leftover with a current card uses one current card and yields one pair. If we instead didn't pair that leftover, leftover from previous would be unmatched, and we could pair current internally if even, which uses two current cards to give one pair. So the trade-off: using one current card to pair with previous leftover gives one pair using 2 cards (one from previous, one current). Not pairing leaves the previous leftover unmatched, and we can pair two current cards internally for one pair using only current cards. But we lose the previous card. So which is better? Compare:
Option A: Pair previous leftover (1 card) with one current card => gain 1 pair, using 2 cards (1 previous, 1 current). Remaining current cards: A_i - 1.
Option B: Do not pair previous leftover (so it remains unmatched), and instead pair current cards internally: we can make floor(A_i/2) pairs. Option A yields: 1 + floor((A_i-1)/2) pairs? Wait after using one current card to pair with previous leftover, we have A_i -1 cards left, which can be paired internally: floor((A_i-1)/2) pairs. Also we need to account for previous internal pairs from earlier? That's same. So total from this step (including previous leftover handling) is: Option A: 1 + floor((A_i-1)/2). Option B: 0 (from previous leftover) + floor(A_i/2). The difference: Option A - Option B = 1 + floor((A_i-1)/2) - floor(A_i/2). Let's compute:
If A_i is even: A_i = 2k. Then floor(A_i/2)=k; floor((2k-1)/2)=k-1. So diff = 1 + (k-1) - k = 0. So equal.
If A_i is odd: A_i = 2k+1. Then floor(A_i/2)=k; floor((2k)/2)=k. So diff = 1 + k - k = 1. So Option A yields 1 more pair than Option B when A_i is odd; when even, tie.
Thus it's never worse to pair the previous leftover, and strictly better if A_i is odd. So greedy of always pairing previous leftover if current has at least one card is optimal.
Now, after pairing previous leftover (if any), we have some number of cards left at i (maybe zero). Then we can form internal pairs: floor(remaining/2) pairs, and leftover = remaining % 2. That leftover will be passed to next pile.
But is there any scenario where we might want to not pair previous leftover, to instead have more leftover to combine with next? Actually, leftover from current is at most 1, so it's a binary state. Could we ever want to have two leftovers? No, because we can only carry over 0 or 1. Our greedy produces leftover = (A_i - used_for_cross) % 2. But could we sometimes benefit from not pairing the previous leftover to have two cards leftover? Wait, we cannot have two leftovers from a pile because we can only pass at most 1? Actually, we could pass more than 1? Let's think: we process sequentially, and after handling cross with previous, we pair internal as much as possible. The leftover is the remainder after internal pairing, which is either 0 or 1 because we pair in twos. So indeed leftover can only be 0 or 1. So state is small.
But is it always optimal to pair internal as much as possible after handling cross? Suppose we have current pile with A_i cards, after possibly using one for cross, we have A_i' cards. Could it be beneficial to not pair some of them internally to keep more cards for cross with next? But cross with next uses one from current and one from next. If we leave an extra card unpaired internally, that means we have an additional leftover (beyond the typical 0/1) to pass to next? But we cannot pass more than 1, because we could decide to not pair internally some pairs to have a leftover of 2? But then we could treat that as 2 cards leftover, and they could be used for cross with next? But cross with next uses one card from current, not two. If we have 2 leftovers, we could use one to pair with next, and the other may remain unmatched or be paired later? But we could also pair the second with next as well if next has many cards. Actually, if we have two cards at i leftover (not internally paired), then we could potentially form two cross pairs with i+1 if i+1 has at least 2 cards. That would yield 2 pairs, vs if we had internally paired those two, we would get 1 internal pair and possibly also cross pairs with leftover? But internal pair uses two cards to get one pair, while cross pairs each use one from i and one from i+1 to get one pair. So if i+1 has enough cards, using two cross pairs yields 2 pairs using 2 from i and 2 from i+1, while internal + maybe cross with leftover (if any) might yield less? Let's test: A_i=2, A_{i+1}=2. Option 1: internal pair at i (1 pair), leftover 0; then at i+1, internal pair (1 pair) total 2. Option 2: don't pair internally at i; leave both cards. Then at i+1, we can cross pair both: two cross pairs, total 2. Same.
What about A_i=2, A_{i+1}=3? Option 1: internal i =>1, leftover0; then i+1: internal floor(3/2)=1, leftover1 => total 2. Option 2: no internal i => cross: we can pair min(2,3)=2 cross pairs (using 2 from i, 2 from i+1). Then i+1 left =1, which can be internal? No, 1 leftover unmatched. Total pairs=2. Same.
A_i=2, A_{i+1}=1: Option1: internal i=1, i+1: leftover0? Actually i+1=1, internal 0, total 1. Option2: cross pair 1 (since min(2,1)=1) => 1 pair, i left 1 (unmatched), i+1 left 0 => total 1. Same.
A_i=2, A_{i+1}=0: Option1: internal i=1, total1; Option2: no cross, internal i=1 total1. So same.
Thus for two piles, it seems internal vs cross allocation doesn't matter in total pairs, as long as we pair as many cards as possible overall? Let's verify more systematically: For two piles with counts a and b, maximum pairs is floor((a+b)/2) but subject to not pairing across? Actually allowed pairs: (i,i), (i,i+1), (i+1,i+1). So we can consider this as a small graph: two vertices with self-loops and an edge between them. The maximum matching size in such a graph? Since cards are identical, we just need to maximize number of pairs. Equivalent to: we want to maximize x (internal pairs at i) + y (internal at i+1) + z (cross pairs) subject to:
2x <= a, 2y <= b, and 2z <= a - 2x + b - 2y? Actually cross pairs use one from each pile, so we need:
x, y, z >= 0 integers.
Constraints: 2x + z <= a (since x internal pairs use 2x cards from pile i, and z cross pairs use z cards from pile i). Similarly, 2y + z <= b.
We want to maximize x+y+z.
Given a,b, the maximum is floor((a+b)/2) because the total cards is a+b and each pair uses two cards, but is there any restriction preventing achieving floor((a+b)/2)? The only restriction is that cross pairs require one from each pile, so we cannot pair more than min(a,b) cross pairs? But internal pairs can use leftover. Indeed, the maximum is floor((a+b)/2) because we can always arrange to pair all but possibly one card. For two piles, we can always achieve floor((a+b)/2). For example, if both are odd, we have one leftover. So it's achievable. So any allocation that uses as many cards as possible yields floor((a+b)/2). Our greedy method for two piles would give: if we process i then i+1, at i: internal floor(a/2), leftover a%2. Then at i+1: first pair leftover if any and b>0 => cross if leftover=1 and b>=1, then internal floor(b'/2) where b'=b - used. Total pairs = floor(a/2) + (a%2 if b>0? ) + floor((b - a%2)/2). Let's compute total: floor(a/2) + a%2? Actually if a%2=1 and b>=1, we get one cross pair, so that adds 1. Then we have b' = b-1, internal floor((b-1)/2). So total = floor(a/2) + 1 + floor((b-1)/2). If a even, total = floor(a/2) + floor(b/2) (since no cross). This equals floor((a+b)/2) because:
- If a even: a=2k, total = k + floor(b/2). Since floor((2k+b)/2) = k + floor(b/2) if b integer? Actually (2k+b)/2 = k + b/2; floor = k + floor(b/2) when b even? If b odd, (2k+b) odd? 2k+b odd => floor = k + (b-1)/2 = k + floor(b/2). So yes.
- If a odd: a=2k+1, b>=1. total = k + 1 + floor((b-1)/2) = k+1+ floor((b-1)/2). Note floor((2k+1+b)/2) = floor((2k + b +1)/2) = k + floor((b+1)/2). For b odd: b=2m+1 => floor((b+1)/2) = floor((2m+2)/2)=m+1. Our total = k+1+ floor((2m+1-1)/2)=k+1+m = k+m+1. Good. For b even: b=2m => floor((b+1)/2)= floor((2m+1)/2)=m. Our total = k+1+ floor((2m-1)/2)=k+1+(m-1)=k+m. So matches? Wait for b even: total from greedy: a=2k+1, b=2m. Then floor(a/2)=k, leftover=1, cross pair uses 1 from b, b'=2m-1, internal floor((2m-1)/2)=m-1. Total = k + 1 + (m-1) = k+m. floor((a+b)/2)= floor((2k+1+2m)/2)= floor((2k+2m+1)/2)= k+m (since 2k+2m even, plus 1 gives .5 floor = k+m). So matches. So greedy for two piles achieves optimum.
Now, for more than two piles, the greedy that processes left to right, always pairing previous leftover if possible, and then internal pairs, should be optimal. But is there any scenario where it's better to intentionally not pair some cards internally to leave two cards for cross with next? For three piles i, i+1, i+2, we need to consider interactions.
Consider A_i, A_{i+1}, A_{i+2}. Suppose A_i is large even, A_{i+1}=1, A_{i+2} large even. Greedy: At i: internal pairs floor(A_i/2), leftover 0 (since even). i+1: previous leftover=0, internal 0 (since 1), leftover 1. i+2: previous leftover=1, cross pair with one from i+2 (so use 1), then internal floor((A_{i+2}-1)/2), leftover maybe 0 if A_{i+2} odd? But A_{i+2} even, say 2k. Then after cross, left =2k-1, internal floor((2k-1)/2)=k-1, leftover 1. So total pairs = floor(A_i/2) + 1 (cross) + (k-1) = floor(A_i/2) + k. That's floor(A_i/2) + floor(A_{i+2}/2) (since k = A_{i+2}/2). So total = floor(A_i/2) + floor(A_{i+2}/2). Could we do better by pairing the lone card at i+1 with i rather than with i+2? For instance, at i: we could not pair some internal to leave a leftover to pair with i+1. If A_i is even, we could purposely leave one card unpaired from internal to pair with i+1. Then i would have leftover 1 after internal? Actually, to have a leftover from i to pair with i+1, we need to ensure after internal pairing we have odd number. If A_i is even, we could decide to not make one internal pair, so we have 2 leftover (if we skip one pair). But then we could pair one of those with i+1, leaving maybe one leftover at i. Let's simulate: A_i = 2k (even), A_{i+1}=1, A_{i+2}=2m (even). Option A (greedy) gave: pairs = k + m. Option B: At i, we make only k-1 internal pairs (using 2(k-1) cards), leaving 2 cards unpaired. Then we can pair one of those with i+1 (cross), using one from i and the one from i+1, giving 1 cross pair. Now i leftover = 1 (the other from i). i+1 consumed, so i+1 has 0. Then proceed to i+2: we have previous leftover from i =1. We can pair it with one from i+2 (cross), giving another cross pair. Then i+2 left = 2m-1. Internal pairs: floor((2m-1)/2)=m-1, leftover 1. Total pairs = (k-1) internal from i + 1 cross (i & i+1) + 1 cross (i & i+2) + (m-1) internal from i+2 = k-1+1+1+m-1 = k+m. Same total. So no improvement.
What if A_{i+2} is odd? Let's test variations. Generally, the greedy seems to achieve optimal. This problem is essentially maximizing the number of matches in a path with self-loops, which can be solved by a simple greedy or DP. However, we need to be careful with large numbers: we cannot simulate each card individually because A_i up to 1e9, but our greedy only uses arithmetic operations per i.
The greedy algorithm:
prev_rem = 0 # leftover cards from previous pile (0 or 1, but may be more? Actually from previous step we only have 0 or 1)
total_pairs = 0
for i from 1 to N:
a = A_i
# first use prev_rem to pair with current if possible
if prev_rem > 0 and a > 0:
# pair one from current with prev_rem
total_pairs += 1
a -= 1
prev_rem = 0 # the previous leftover is used
# now internal pairs
pairs_i = a // 2
total_pairs += pairs_i
prev_rem = a % 2 # leftover after internal pairs
But wait: is it always correct to set prev_rem=0 after pairing? Actually, if prev_rem >0, we pair exactly one card from current with it. But what if prev_rem >1? Could prev_rem be >1? In this scheme, we only ever carry over 0 or 1. So it's fine.
But is it possible that prev_rem could be more than 1? In the greedy above, we only keep at most 1 leftover. But could there be cases where we need to carry over more than 1 to achieve optimum? For example, suppose A_i is huge, and we want to keep more than one card to pair with i+1? But i+1 can only pair with i's cards one-to-one, so if we have many cards at i, we can pair as many as min(available from i, A_{i+1}) cross pairs. In greedy, cross pairs are formed only from leftover from previous? But that only accounts for at most 1 cross pair between i and i+1 in this step, because we only use the leftover from i to pair with i+1. However, we might be able to create more than 1 cross pair between i and i+1 if both piles have many cards. For instance, A_i=5, A_{i+1}=5. We could create up to 5 cross pairs theoretically (using all cards). But our greedy as described would only create at most 1 cross pair (from leftover of i) and then internal pairs for the rest. That would yield: at i: internal pairs floor(5/2)=2, leftover 1; then at i+1: pair leftover with one from i+1 (cross) => 1, then internal floor((5-1)/2)=2, leftover 1. Total pairs =2+1+2=5. But maximum possible pairs is floor((5+5)/2)=5, so it's optimal. However, cross pairs count =1, but total pairs is same. So greedy yields optimum total pairs, even if cross pair count is low.
But could there be a scenario where forming more cross pairs instead of internal pairs leads to a higher total? Let's test: A_i=3, A_{i+1}=3. Greedy: i internal 1, leftover 1; i+1 cross 1, internal floor(2/2)=1, leftover 0 => total=1+1+1=3 (max). Alternative: no internal at i, all cross: min(3,3)=3 cross pairs, total=3. Same.
A_i=4, A_{i+1}=3. Greedy: i internal 2, leftover 0; i+1 internal floor(3/2)=1, leftover 1 => total=3. Max possible floor((7)/2)=3. OK.
A_i=4, A_{i+1}=4. Greedy: 2+0? Actually i internal 2, leftover 0; i+1 internal 2, leftover 0 => total=4. Max 4.
A_i=1, A_{i+1}=2, A_{i+2}=1. Greedy: i: internal 0, leftover 1; i+1: cross 1 (using prev leftover), a becomes 2-1=1, internal 0, leftover 1; i+2: cross 1 (using prev leftover), a becomes 1-1=0, internal 0, leftover 0. Total = 2. Max floor((1+2+1)/2)=2. OK.
Now, consider a case where we might want to "reserve" a card from i for cross with i+1, but we also have leftover from i-1. Greedy handles that.
Thus the greedy algorithm appears correct. However, we need to be absolutely sure. This is essentially the classic problem of "maximum matching in a path with multiplicities" or "maximum number of pairs such that each card can pair with itself or neighbor". It's similar to "AtCoder ABC 139 D? Not sure. Actually AtCoder problem "Snuke's Coloring"? No.
We can think of it as: we want to maximize number of pairs. Since each pair uses two cards, maximum is floor(total/2). But restrictions might prevent achieving that if there is some bottleneck? For example, if we have a single pile with many cards, we can always pair them internally, so floor(total/2) is achievable if all cards can be paired with each other? But wait, cross pairs only between adjacent numbers. Could there be a situation where total cards is even but we cannot pair them all because of adjacency constraints? Consider N=2, A1=1, A2=1: total=2, we can pair cross, so 1 pair. OK. Consider N=3, A1=1, A2=0, A3=1: total=2, but can we pair them? Pair (1,1) can't because only one card of 1; (3,3) can't; cross: difference between 1 and 3 is 2 >1, not allowed. So we cannot pair them. So maximum pairs is 0, while floor(total/2)=1. So adjacency restrictions can reduce. So we need to consider the connectivity.
Our greedy should account for this: For N=3, A=[1,0,1], our algorithm: i=1: internal 0, leftover 1; i=2: a=0, prev leftover=1 but a=0 so cannot pair; total unchanged, prev_rem remains 1? Actually we should handle: if prev_rem>0 and a>0 then we pair, else we cannot. In i=2, a=0, so no pair, prev_rem stays 1? But then after i=2, we compute internal pairs=0, leftover=0? Actually after checking cross, we compute internal pairs on a (which is 0) => internal 0, leftover 0. But we should keep prev_rem from previous? Wait, our algorithm above sets prev_rem = a % 2 after internal pairs, but we also need to consider that if we didn't use prev_rem, it persists. Let's re-evaluate: Our state should be: after processing pile i, we have some number of leftover cards from pile i (0 or 1) that can be used for pairing with i+1. But what about leftover from earlier piles that were not used? They cannot be carried over beyond the immediate next pile because they can only pair with i (if from i-1) or i+1? Actually leftover from pile i-1 that wasn't paired at i becomes permanently unmatched because we have moved past i. So if at step i we have prev_rem (from i-1) and we cannot pair it because A_i = 0, then that prev_rem is lost (unmatched). So after processing i, we should not carry that prev_rem forward; it is gone. So our state after i should only reflect leftover from pile i, not from previous. Therefore in algorithm, we need to treat prev_rem as leftover from i-1 that must be used now or discarded. So we should not keep it if not used. So the algorithm is:
prev_rem = 0
total = 0
for i in 1..N:
a = A_i
# try to use prev_rem with current pile
if prev_rem > 0 and a > 0:
total += 1
a -= 1
prev_rem = 0 # used
# now internal pairs on remaining a
total += a // 2
prev_rem = a % 2 # leftover from current pile for next
Note: If prev_rem > 0 and a == 0, then we cannot use it, so it's simply discarded. So we set prev_rem = 0 after processing? Actually we don't use it, so it's lost. So we then compute internal pairs on a (0), so prev_rem becomes 0. That's correct.
So for A=[1,0,1], we get: i=1: a=1, prev_rem=0 -> no cross; internal 0, prev_rem=1. total=0.
i=2: a=0, prev_rem=1, but a=0 so cannot cross; internal 0, prev_rem=0; total remains 0.
i=3: a=1, prev_rem=0 -> no cross; internal 0, prev_rem=1; total=0. So output 0. Good.
Now test with provided examples:
Example1: N=4, A=[4,0,3,2]
i=1: a=4, prev=0 -> internal 2, prev=0; total=2.
i=2: a=0, prev=0 -> internal 0, prev=0; total=2.
i=3: a=3, prev=0 -> internal 1, prev=1; total=3.
i=4: a=2, prev=1 -> cross: total=4, a=1; internal floor(1/2)=0, prev=1; total stays 4. Output 4. matches.
Example2: N=8, A=[2,0,1,6,0,8,2,1]
Let's compute:
i1: a=2, prev=0 -> internal 1, prev=0, total=1.
i2: a=0, prev=0 -> total=1.
i3: a=1, prev=0 -> internal 0, prev=1, total=1.
i4: a=6, prev=1 -> cross: total=2, a=5; internal 5//2=2, prev=1, total=4.
i5: a=0, prev=1 -> cannot cross, a=0, internal0, prev=0, total=4.
i6: a=8, prev=0 -> internal 4, prev=0, total=8.
i7: a=2, prev=0 -> internal 1, prev=0? Actually a=2: internal 1, prev=0, total=9.
i8: a=1, prev=0 -> internal 0, prev=1, total=9.
Output 9. matches.
So greedy works for examples.
But we need to be absolutely sure it's optimal for all cases. Let's attempt a formal proof or consider potential counterexample.
We can model as a flow or DP with state being leftover count from previous pile (0 or 1). The decision at each pile i: given incoming leftover L (0 or 1) and pile size A_i, we need to decide how many cross pairs with previous (i-1,i) to make, which consumes up to min(L, A_i) cards, but L is at most 1. Actually L is either 0 or 1. So cross pairs with previous are either 0 or 1. Then after that, we have some number of cards left in pile i, call B. From these B cards, we can form internal pairs (within i) and also we can decide to leave some unpaired to be used as leftover for next pile. But since we can only leave at most 1 (because leftover for next can only be 0 or 1 if we want to maximize? Actually we could leave more than 1, but would that ever be beneficial? If we leave k>1 cards, they could be used for cross pairs with i+1. But cross pairs with i+1 can use at most A_{i+1} cards from i, so the number of cross pairs possible between i and i+1 is limited by min(available from i, A_{i+1}). If we leave many cards at i, we might form more cross pairs with i+1. However, internal pairs at i also yield pairs. So we need to compare: leaving k cards at i (unpaired) means we didn't get floor(k/2) internal pairs at i, but we might get up to min(k, A_{i+1}) cross pairs with i+1. However, those cross pairs also use cards from i+1, reducing internal pairs at i+1. So it's a trade-off.
In our greedy, we always pair internal as much as possible after dealing with previous leftover, leaving at most 1. But could it be better to leave, say, 2 cards unpaired at i to get 2 cross pairs with i+1? Compare two strategies for two piles i and i+1 with counts a and b.
Let a = A_i, b = A_{i+1}. Suppose we have no leftover from i-1. We can decide to leave x cards at i unpaired (0 <= x <= a) to be used for cross with i+1. The rest a-x are used for internal pairs at i, yielding floor((a-x)/2) internal pairs. Then at i+1, we can use up to min(x, b) cross pairs. After that, i+1 has b - min(x,b) cards left, which can be paired internally yielding floor((b - min(x,b))/2) internal pairs. Also note that cross pairs consume one from i and one from i+1. The total pairs is: floor((a-x)/2) + min(x,b) + floor((b - min(x,b))/2). We want to maximize over integer x between 0 and a.
We can analyze this function. Let t = min(x,b). Since x <= a, and also x can be at most b to be fully used, but could be more. But we can think of x as number of cross pairs we aim for, but limited by b and a. Actually, the number of cross pairs z satisfies 0 <= z <= min(a,b). We can decide to make z cross pairs. Then we use z cards from i and z from i+1. The remaining cards at i: a - z, at i+1: b - z. From those, we can make internal pairs: floor((a-z)/2) + floor((b-z)/2). So total pairs = z + floor((a-z)/2) + floor((b-z)/2). This is the objective. Note that this formulation assumes we don't have any leftover from previous or to next beyond these two piles (i.e., we consider only these two piles; but in a chain, leaving leftover from i+1 could affect i+2). However, we can think in terms of dynamic programming with state being leftover from i+1 after internal pairing, which is (b-z) mod 2. So the choice of z influences the leftover passed to i+2.
So the problem reduces to: for each i, given incoming leftover L (0 or 1) from i-1, we need to decide how many cross pairs to make between i-1 and i (which is essentially min(L, something)? Actually L is either 0 or 1, so cross with previous is either 0 or 1, not a variable. Then after that, we have A_i' = A_i - used_in_cross_with_prev. Then we need to decide how many cross pairs to make with i+1, i.e., choose z (0 <= z <= min(A_i', A_{i+1}) perhaps? But we also have internal pairs at i. However, the decision of cross with i+1 interacts with i+1's own decisions. So a DP with state being leftover from i is natural.
Let's define DP[i][r] = maximum pairs we can form using piles 1..i, with r cards left unpaired from pile i (r = 0 or 1) that can be used to pair with pile i+1. We have to consider that r can be more than 1? But maybe we can restrict to 0 or 1 because any more would be suboptimal? Let's examine if r can be 2. Suppose we have A_i = 100, and we want to leave 2 cards for cross with i+1 because A_{i+1} is large. But could leaving 2 yield more total than leaving 0 or 1? Consider only two piles i and i+1, we saw that any choice of z (number of cross pairs) yields total pairs = z + floor((a-z)/2) + floor((b-z)/2). Let's see if the leftover from i after all pairs (including cross and internal) is (a-z) mod 2. So r_i = (a-z) mod 2, which is either 0 or 1. So after we have decided cross pairs z, the leftover from i is either 0 or 1, not more. Because any remaining cards after using z for cross and internal pairing in groups of 2 will leave remainder 0 or 1. So r_i is always 0 or 1. So we don't need to consider r_i > 1. Good.
Similarly, leftover from i-1 that is passed to i is either 0 or 1. So DP state size is 2.
Thus we can DP:
Let DP[i][r] = max pairs using first i piles, with r leftover cards from pile i (r = 0 or 1). Transition from i-1 to i:
We know DP[i-1][s] where s = 0 or 1. Then we have A_i. We need to decide how many cross pairs between pile i-1 and i to make. Since s is the number of leftover cards from i-1, the maximum cross pairs we can make is min(s, A_i). But s is 0 or 1, so cross pairs count c is either 0 (if s=0 or A_i=0) or 1 (if s=1 and A_i>=1). So c = 1 if s=1 and A_i>0, else 0.
After using c cards from A_i, we have A_i' = A_i - c. Now from A_i' we need to decide internal pairing and possibly leave some for next (r). But the number of internal pairs we make from A_i' is floor((A_i' - r)/2)? Actually we want to end with exactly r leftover cards from pile i (r=0 or 1). So given A_i' and desired r, we can compute how many internal pairs we can make: let used for internal pairs be 2 * p, where p is number of internal pairs. Then we must have A_i' - 2p = r, so p = (A_i' - r) / 2, which must be integer >=0. So for given A_i' and r, the number of internal pairs is (A_i' - r)/2 if A_i' >= r and (A_i' - r) is even. If not, that (r) state is infeasible.
Thus total pairs from transition: DP[i-1][s] + c + (A_i' - r)/2.
We need to maximize over s=0,1 and r=0,1 with feasibility.
But note that after using c cross pairs, we also have that the leftover from i-1 (s) is fully used if c=1; if s=1 but we didn't use it (c=0) because A_i=0, then that s leftover is lost? Actually if s=1 and A_i=0, then we cannot pair it; the leftover from i-1 would be discarded, but we still have s=1 in the state from previous? In the DP, we should not allow that because if s=1 and we don't use it, that means that card remains unmatched, which is not represented in DP[i][r] because we are not counting unmatched cards from i-1. But DP[i-1][s] already accounted for pairs up to i-1, and s indicates leftover from i-1 that are still available for pairing with i. If we cannot pair it with i, those s cards become unmatched and we effectively lose them. That transition should still be allowed, but the total pairs would not increase, and the leftover from i becomes based on A_i. So we can still have transition where c=0 even if s=1 and A_i=0. In that case, DP[i][r] = DP[i-1][s] + 0 + (A_i - r)/2, but we must ensure that (A_i - r) is even and >=0. Also, we must consider that the s leftover is not used, so it becomes unmatched; that is fine because DP[i-1][s] already accounted for it as leftover that could be used, but if it's not used, it's a waste. But is that allowed in the DP definition? DP[i-1][s] gives maximum pairs for first i-1 piles with s leftover from pile i-1. That leftover is still present; when we move to i, if we don't use it, those leftover cards are effectively discarded, and we don't get any pair from them. That transition is valid, but it might be suboptimal because we could have maybe used it if possible. But if it's not possible (A_i=0), we have no choice. So DP should allow it. So the transition should be: for each s, r:
c = 0 if s=0 or A_i=0? Actually if s=1 and A_i>0, we have a choice: we can either use it (c=1) or not use it (c=0). Is it ever beneficial to not use a leftover when we can? Earlier we argued that using leftover is never worse than not using it if A_i>0, because it gives at least as many pairs (strictly better if A_i odd). Let's verify with DP: If we use it (c=1), A_i' = A_i-1, then we can have some r. If we don't use it (c=0), A_i' = A_i. Compare total pairs: Using: DP[i-1][s] + 1 + (A_i-1 - r)/2. Not using: DP[i-1][s] + 0 + (A_i - r')/2, with r' possibly different to maintain parity? But we can choose r and r' independently (0 or 1). So we need to see maximum achievable. Suppose A_i is odd. For using leftover, we have A_i-1 becomes even. For not using, A_i odd. The best we can do for each case: For using: best is r=0 because (A_i-1) even => internal pairs (A_i-1)/2, total = 1 + (A_i-1)/2 = (A_i+1)/2. For not using: best is maybe r=1 because (A_i -1) even? Actually if A_i odd, we can set r=1 to make (A_i -1) even => internal pairs (A_i-1)/2, total = (A_i-1)/2. So using gives one more pair. So using is better. If A_i even: Using gives total = 1 + (A_i-2 - r)/2? Let's see: A_i even, A_i-1 odd. Best for using: set r=1 => (A_i-1-1)/2 = (A_i-2)/2, total = 1 + (A_i-2)/2 = A_i/2. For not using: A_i even, best set r=0 => A_i/2. So tie. So using is never worse. So in DP we can safely always use the leftover if possible. But for completeness, we can include both and the DP will naturally choose max, but we can simplify.
Thus the greedy that always uses leftover if possible is consistent with DP optimal.
Now, after using c, we have A_i' = A_i - c. Then we need to choose r (0 or 1) such that (A_i' - r) is even and nonnegative. That is always possible: if A_i' is even, we can set r=0; if odd, set r=1. So r = A_i' % 2. That's exactly what greedy does: after pairing cross if any, we set leftover = (remaining) % 2, and internal pairs = (remaining - leftover)/2. So greedy is essentially performing the DP with the optimal decisions at each step (always using leftover, and then choosing r as the parity). Since DP state is only dependent on previous state, greedy yields optimal.
Thus the greedy algorithm is correct.
But we need to verify that we aren't missing any possibility of having leftover from i-1 >1. Could it be beneficial to not pair some internal pairs at i-1 to have more than 1 leftover? For example, suppose A_{i-1} = 3, A_i = 0, A_{i+1} = 3. Our greedy: i-1: internal 1, leftover 1; i: a=0, prev_rem=1, can't pair, so leftover from i-1 lost, prev_rem becomes 0; i+1: a=3, internal 1, leftover 1. Total = 2. Could we do better? If we didn't pair internally at i-1, we could have 3 cards at i-1. Since i=0, we cannot pair any with i, but we could maybe pair with i+1? However i-1 and i+1 are not adjacent (difference 2), so not allowed. So we must pair within i-1 or with i. Since i=0, the only option is internal at i-1: max pairs = floor(3/2)=1, leftover 1. So total from i-1 and i+1 = 1 + floor(3/2)=2. So optimal is 2.
Now consider A_{i-1}=4, A_i=1, A_{i+1}=4. Greedy: i-1: internal 2, leftover 0; i: a=1, prev=0 -> internal 0, leftover 1; i+1: a=4, prev=1 -> cross 1, a=3, internal 1, leftover 1 -> total =2+0+1+1=4? Actually compute: i-1: 2 pairs, i:0, i+1: cross1 + internal1 =2, total=4. Alternative: could we do better? Perhaps at i-1 we could leave more cards to pair with i, and i with i+1, etc. Let's try: Option: at i-1, internal 1 (using 2 cards), leftover 2? Actually we can't have leftover 2 if we internal pair 1, but we can decide not to pair some cards internally: we have 4 cards, we can pair 0 internally, leaving 4. Then we can pair one with i (cross) using 1 from i-1 and the 1 from i => 1 pair. Now i-1 left 3, i left 0. Then i-1's 3 can be paired internally: 1 pair, leftover 1. So far pairs: cross:1, internal:1 =2. Then i+1=4, we can pair the leftover 1 from i-1 with i+1? No, i-1 leftover cannot pair with i+1 because not adjacent. So only i+1 internal: floor(4/2)=2, total=4. Same.
What if we use two cross pairs? Could we have cross pairs between i-1 and i (1 max because i only has 1) and cross between i and i+1? That would require i to have 2 cards, but it only has 1. So total cannot exceed 4. So greedy optimal.
Now consider a longer chain: maybe there is a scenario where it's better to not use a leftover from i-1 even though A_i > 0, in order to have an even number at i to avoid a leftover that would later be wasted? For instance, suppose A_i is odd and we have prev_rem=1. If we use it, A_i' becomes even, and we set leftover 0. If we don't use it, A_i remains odd, we may set leftover 1. So the decision affects the leftover passed to i+1. Let's test a case where using the leftover leads to a suboptimal result due to future impact.
We need to find if there exists A_i, A_{i+1}, ... such that:
Option A: Use leftover (c=1) at i -> results in r_i = 0 (since A_i-1 even).
Option B: Not use leftover (c=0) at i -> results in r_i = 1 (since A_i odd).
Then the future DP may yield different totals. So we need to compare DP from i+1 onward given state r_i=0 vs r_i=1. Could r_i=1 lead to higher overall pairs than r_i=0, even though Option B loses the immediate cross pair? So we need to see if it's possible that sacrificing one immediate pair (by not using the previous leftover) could allow an extra pair later that compensates and yields a net gain.
We already argued that using leftover is never worse when considering only i and i+1? But maybe further lookahead shows benefit.
Consider A_i odd and A_{i+1}=0, A_{i+2} large. Let's design: A_{i-1} leftover 1 (s=1). A_i = 1 (odd). A_{i+1}=0. A_{i+2}= large, say 2.
If we use leftover at i: cross pair (1), A_i'=0, r_i=0. Then i+1: a=0, prev_rem=0 -> internal 0, r=0. i+2: a=2, prev=0 -> internal 1, r=0. Total from i onward: 1 (cross) + 1 (internal at i+2) = 2.
If we do not use leftover at i: no cross, a_i=1, internal 0, r_i=1. i+1: a=0, prev_rem=1 can't pair -> r=0. i+2: a=2, prev=0 -> internal 1, r=0. Total from i onward: 0 + 1 = 1. So using is better.
What about A_i odd, A_{i+1}=1, A_{i+2}=0?
Using: i: cross 1, A_i'=0, r_i=0. i+1: a=1, prev=0 -> internal 0, r=1. i+2: a=0, prev=1 cannot pair -> total: 1 (cross) + 0 =1.
Not using: i: no cross, a_i=1, internal 0, r_i=1. i+1: a=1, prev=1 -> cross 1, a=0, r=0. i+2: a=0, prev=0 ->0. Total: 0 (i) +1 (cross at i+1) =1. So tie.
What about A_i=1, A_{i+1}=2, A_{i+2}=1.
Using: i: cross 1, a_i'=0, r_i=0. i+1: a=2, prev=0 -> internal 1, r=0. i+2: a=1, prev=0 -> internal 0, r=1. total =1+1=2.
Not using: i: no cross, a_i=1, internal 0, r_i=1. i+1: a=2, prev=1 -> cross 1, a=1, internal 0, r=1. i+2: a=1, prev=1 -> cross 1, a=0, r=0. total =0 +1 (cross i+1) +1 (cross i+2) =2. Tie.
What about A_i=1, A_{i+1}=1, A_{i+2}=1.
Using: i: cross 1, a_i'=0, r_i=0. i+1: a=1, prev=0 -> internal0, r=1. i+2: a=1, prev=1 -> cross1, total =1 (i) +1 (i+2) =2.
Not using: i: no cross, a_i=1, internal0, r_i=1. i+1: a=1, prev=1 -> cross1, a=0, r=0. i+2: a=1, prev=0 -> internal0, r=1. total =1 (i+1) =1. So using better.
What about A_i=3, A_{i+1}=0, A_{i+2}=3.
Using: i: cross 1, a_i'=2, internal1, r_i=0. total from i: 1+1=2. i+1:0, i+2:3, prev0 -> internal1, r=1? Actually a=3, internal floor(3/2)=1, r=1. Total =2+1=3.
Not using: i: a=3, internal1, r_i=1 (since 3 odd). total from i:1. i+1:0, prev1 -> cannot pair, r=0. i+2:3, prev0 -> internal1, r=1. Total =1+1=2. Using better.
What about A_i=3, A_{i+1}=1, A_{i+2}=3.
Using: i: cross1, a_i'=2, internal1, r_i=0. i+1: a=1, prev0 -> internal0, r=1. i+2: a=3, prev1 -> cross1, a=2, internal1, r=0. Total pairs: i:2, i+1:0, i+2:2 => total 4.
Not using: i: a=3, internal1, r_i=1. i+1: a=1, prev1 -> cross1, a=0, r=0. i+2: a=3, prev0 -> internal1, r=1. total =1+1+1=3. Using better.
Seems using is never worse. But we need a scenario where using leads to r_i=0 and that 0 leftover prevents a later cross that would have yielded an extra pair, whereas not using leads to r_i=1 which enables a later cross that yields +1, while losing the immediate cross also -1, so net 0. But could there be a scenario where not using yields +2 later? That would require that the leftover from i, if 1, can be used to create two additional pairs? No, a leftover can only contribute at most 1 cross pair with i+1, yielding at most +1 relative to if there was no leftover. So the maximum gain from having a leftover is 1 (if it leads to an extra cross). Meanwhile, not using the previous leftover loses at most 1 immediate cross. So net gain cannot be positive; best is break-even. But could the leftover also affect internal pairing at i+1? For instance, having a leftover may change parity of cards at i+1 after cross, influencing internal pairs. That could potentially change total by more than 1? Let's analyze.
General comparison for two consecutive steps: Given state s (0 or 1) from i-1, we need to decide c (0 or 1) and then from A_i we determine r (0 or 1). The total pairs from i onward (including i and later) can be expressed as DP[i][r] plus the immediate cross. So we need to see which c yields larger DP[i][r] + c.
We can compute DP[i][r] recursively. But we suspect that always c = min(s, A_i) (i.e., use if possible) is optimal. Let's attempt to prove via DP optimality: For a given i, given s, the optimal value is max over c in {0,1} with c <= s and c <= A_i (since cannot use if A_i=0) and then for each possible r, total = DP[i-1][s] + c + (A_i - c - r)/2, with r such that (A_i - c - r) even and >=0. Since (A_i - c) parity determines possible r (r must equal (A_i - c) mod 2). So there is exactly one feasible r for each c (r = (A_i - c) % 2). So we have two candidates:
If we use c=1 (feasible if s=1 and A_i>=1): r1 = (A_i-1) % 2, value1 = DP[i-1][1] + 1 + (A_i-1 - r1)/2 = DP[i-1][1] + 1 + floor((A_i-1)/2). Because (A_i-1 - r1)/2 = floor((A_i-1)/2) since r1 = (A_i-1) mod 2.
If we use c=0 (always feasible): r0 = A_i % 2, value0 = DP[i-1][s] + 0 + (A_i - r0)/2 = DP[i-1][s] + floor(A_i/2).
So we need to compare value1 vs value0, assuming s=1 and A_i>=1. Since DP[i-1][1] may be different from DP[i-1][0]? Wait, in value0 we used DP[i-1][s] with s=1? Actually value0 uses the same s (since we didn't use the leftover, so we keep s=1? But careful: In transition, we start with DP[i-1][s]; after deciding c, we add pairs and then the new state r. But the DP[i-1][s] is the maximum pairs achievable for first i-1 piles with leftover s. For value0, c=0, meaning we did not use the leftover from i-1; but that leftover s=1 remains and is effectively discarded because we are moving to i. In DP[i][r], we are not carrying s forward; we only care about leftover from i. So in the transition, we consider the contribution from previous state plus pairs formed at i. So for value0, we use DP[i-1][s] with s=1. For value1, we use DP[i-1][1] as well (since we used the leftover, but the previous state is still DP[i-1][1]). So both use the same DP[i-1][1] (if s=1). So the comparison reduces to:
value1 = DP[i-1][1] + 1 + floor((A_i-1)/2)
value0 = DP[i-1][1] + floor(A_i/2)
Thus value1 - value0 = 1 + floor((A_i-1)/2) - floor(A_i/2). As computed earlier, this is 0 if A_i even, 1 if A_i odd. So value1 >= value0, with strict > when A_i odd.
Thus for s=1, it's optimal to set c=1 if A_i >=1. So indeed using the leftover is optimal regardless of future.
Now consider the case where s=0. Then only c=0 is possible, so no choice.
Thus the DP reduces to a deterministic transition: c = (s==1 and A_i>0) ? 1 : 0; then r = (A_i - c) % 2; and DP[i][r] = DP[i-1][s] + c + (A_i - c - r)/2.
Since s is determined by previous step, we can compute iteratively. The greedy algorithm exactly does this.
Therefore the greedy algorithm yields the optimal DP solution.
Thus the solution is simple: iterate over i from 1 to N, maintain rem (leftover from previous), total pairs. For each A_i:
if rem == 1 and A_i > 0:
total += 1
A_i -= 1
rem = 0
total += A_i // 2
rem = A_i % 2
That's it. Complexity O(N).
Now, we need to be careful about integer sizes: A_i up to 1e9, sum up to 1e14, so total pairs up to 5e13, fits in 64-bit. Use Python int (unbounded) or C++ long long.
Edge cases: N=1: just internal pairs floor(A_1/2). Works.
Now we can produce final answer.
But wait: Are there any scenarios where we might want to not use a leftover even if A_i>0 because s could be more than 1? But we argued s cannot exceed 1 in optimal DP. However, we didn't prove that s cannot be >1 in an optimal solution. But we can argue that any solution can be transformed to one where at most one card is left unpaired from each pile, without decreasing number of pairs. Because if from a pile we have more than one unpaired cards, we could pair two of them together (internal) to get an extra pair. Unless those cards are intended for cross pairs with the next pile? But if there are more than one unpaired, they could be used for cross pairs, but cross pairs only need one from each pile. Having two unpaired from pile i means at most one can be used for cross with i+1 (since i+1 may have many, but cross pairing uses one from i per pair). The second unpaired from i cannot be used in cross with i+1 unless there is also a second from i+1? Actually, if we have 2 unpaired cards from i, we could form up to 2 cross pairs with i+1 if i+1 has at least 2 cards. So leaving 2 unpaired might enable 2 cross pairs, which yields 2 pairs, whereas pairing those two internally yields only 1 pair. So leaving 2 unpaired could be beneficial if i+1 has at least 2 cards. But then, after using them for cross, we might have leftover from i+1? But we could have also paired them internally at i and cross with i+1 using other cards? Need to examine.
Consider A_i = 2, A_{i+1}=2. Option1: leave both unpaired at i: then cross pairs =2, total=2. Option2: internal pair at i (1) and internal at i+1 (1) total=2. So same. If A_i=2, A_{i+1}=3: Option1: cross 2, then i+1 left 1 -> total=2. Option2: internal i=1, cross? Could we do internal i=1, then cross 1 (using leftover from i? but i had 0 leftover), internal i+1=1 (since after cross i+1 left 2? Actually internal i=1 uses 2 cards, leaving 0; then i+1=3 internal gives 1, leftover 1 => total=2. So same.
What about A_i=2, A_{i+1}=4: cross 2 =>2 pairs, i+1 left 2 => internal1 => total3. Internal i=1, internal i+1=2 => total3. Same.
What about A_i=3, A_{i+1}=3: cross 3 =>3 pairs total. Internal i=1, leftover1; cross1; internal i+1=1 => total3. So same.
Thus leaving multiple unpaired doesn't seem to give advantage over internal pairing + cross; total pairs remains the same. But is there a scenario where leaving multiple unpaired yields more total pairs than any strategy that leaves at most 1? Let's try to find counterexample.
We want to maximize total pairs over the whole line. Consider three piles i, i+1, i+2. Suppose A_i = 2, A_{i+1}=1, A_{i+2}=2. Greedy: i: internal1, leftover0; i+1: internal0, leftover1; i+2: cross1, a=1, internal0, leftover1 => total = 1+1=2. Alternative: at i, leave both unpaired (0 internal). Then at i+1 (1), cross1 using one from i and the one from i+1 => 1 pair, i left 1; then at i+2 (2), cross1 using i leftover and one from i+2 => 1 pair, i+2 left 1; no internal possible => total=2. Same.
What about A_i = 3, A_{i+1}=1, A_{i+2}=3. Greedy: i internal1, leftover1; i+1 cross1, a=0, leftover0? Actually i+1: cross1 (since prev1 and a1>0) => total+1, a=0, leftover0; i+2: a=3, prev0 internal1, leftover1 => total =1 (i internal) +1 (cross) +1 (i+2 internal)=3. Alternative: i: no internal, leave 3; i+1: cross1, using 1 from i and 1 from i+1 => i left 2, i+1 0; i+2: a=3. Now we could cross from i (2) with i+2? But i and i+2 are not adjacent, so cannot. So we have i left 2 (can only pair internally now) => internal1, leftover0; i+2:3 internal1, leftover1 => total =1 (cross) +1 (i internal) +1 (i+2 internal) =3. Same.
What about A_i = 4, A_{i+1}=1, A_{i+2}=4. Greedy: i internal2, leftover0; i+1 internal0, leftover1; i+2 cross1, a=3, internal1, leftover1 => total=2+1+1=4. Alternative: i: leave 4, i+1: cross1 => i left3, i+1 0; i+2:4. Now we can cross? i left3 cannot cross with i+2. So i internal floor(3/2)=1, leftover1; i+2 internal floor(4/2)=2, leftover0? Actually internal 2 uses 4, leftover0 => total = cross1 + internal i 1 + internal i+2 2 =4. Same.
What about A_i = 5, A_{i+1}=1, A_{i+2}=5. Greedy: i internal2, leftover1; i+1 cross1 (since prev1), a=0, leftover0; i+2 internal2, leftover1? Actually i+2: a=5, prev0 internal2, leftover1 => total=2+1+2=5. Alternative: i: leave 5; i+1 cross1 => i left4, i+1 0; i+2:5. Then i internal2, leftover0; i+2 internal2, leftover1 => total= cross1 +2+2=5. Same.
Seems always equal. Could there be a case where leaving multiple unpaired yields more? Suppose A_i=2, A_{i+1}=2, A_{i+2}=2. Greedy: i internal1, leftover0; i+1 internal1, leftover0; i+2 internal1, leftover0 => total=3. Alternative: i leave both; i+1: cross2 (using both from i and both from i+1) => 2 pairs; i left 0, i+1 left 0; i+2:2 internal1 => total=3. Same.
Thus leaving multiple unpaired seems not to improve over greedy. Could there be a case where leaving multiple unpaired from i combined with leaving multiple from i+1 yields extra pairs? Possibly if both have many cards, but we already see that total pairs is limited by total cards/2. The only potential loss is when there is isolation of odd cards across non-adjacent piles. The greedy essentially pairs as much as possible locally, but maybe a global optimum might sometimes avoid pairing an even pile's internal pair to create a cross that resolves two odd cards from non-adjacent piles? Actually, consider three piles with counts: A1=1, A2=0, A3=1. Greedy gives 0, which is optimal because no adjacency. What about A1=1, A2=1, A3=1. Greedy: i1 internal0, leftover1; i2: cross1 (since prev1 and a2=1) => total1, a2=0, leftover0; i3: a3=1, internal0, leftover1 => total=1. But is it possible to get 2 pairs? Total cards=3, max pairs floor(3/2)=1, so optimal is 1. Good.
What about A1=2, A2=1, A3=2. Greedy: i1 internal1, leftover0; i2 internal0, leftover1; i3 cross1, a3=1, internal0, leftover1 => total=1+1=2. Could we get 3? total cards=5, floor(5/2)=2, so max 2. Good.
Now consider a scenario where there is an even pile in the middle with many cards, and odd piles on both sides. Greedy will pair internal at middle and cross with sides if possible, but might leave some odd cards on sides unpaired if they are not adjacent. But can we do better by using cross pairs between side and middle to reduce leftover? Let's test: A1=1, A2=2, A3=1. Greedy: i1 internal0, leftover1; i2: cross1 (since prev1 and a2=2) => total1, a2=1, internal0, leftover1; i3: cross1? Actually i3: a3=1, prev1 -> cross1, a3=0, leftover0 => total=2. That's 2 pairs. Total cards=4, max 2. So perfect.
What about A1=1, A2=2, A3=0, A4=1? Greedy: i1:0, leftover1; i2: cross1, a2=1, internal0, leftover1; i3: a3=0, prev1 can't pair -> discard; i4: a4=1, prev0 -> internal0, leftover1 => total=1. Could we do better? Perhaps we should not cross at i2 to save the card for i4? But i2 and i4 are not adjacent, so can't. Let's examine possibilities. Cards: 1 at 1, 2 at 2, 1 at 4. Allowed pairs: (1,1) not possible (only one), (1,2) allowed, (2,2) allowed, (4,4) not possible, (3,4) but no 3, (4,5) no. So we can pair (1,2) ->1, then we have 1 left at 2 (since 2-1=1) and 1 at 4. Those cannot pair because difference 2. Alternatively, pair (2,2) ->1, leaving 1 at 1 and 1 at 4, also no pair. So maximum is 1. Greedy gave 1. Good.
Thus the greedy algorithm appears correct.
Now, but is there any scenario where the greedy algorithm's decision to always pair previous leftover could lead to a situation where we have a leftover from current (0) that later prevents a cross that would have been possible if we had a leftover? Actually, as we saw, using leftover when A_i odd yields r_i=0; not using yields r_i=1. The difference in future is that having r_i=1 could allow an extra cross with i+1, but we already accounted that in value1 vs value0: when A_i odd, using gives immediate +1 and r_i=0; not using gives 0 immediate and r_i=1. The net effect on future: with r_i=1, we may gain up to 1 later, but we already lost 1 now, so net 0. But could the future gain be more than 1? For example, could having a leftover at i lead to not just one extra cross but also affect internal pairs at i+1? Let's analyze: At i+1, if we have prev_rem=1 vs 0, we will pair it if possible. The total pairs from i+1 onward as a function of prev_rem s' (0 or 1) can be described by DP[i+1][*]. We need to see if DP[i+1][...] can be more than 1 higher when s'=1 compared to s'=0. In other words, is the value of having a leftover from previous pile at i+1 exactly 1 in extra potential pairs? Actually, we can compute the difference: The optimal value from pile i+1 onward given state s' (0 or 1) is something like DP_from_i+1(s'). Let's define f(j, s) as maximum pairs from piles j..N given that we have s leftover from pile j-1 to pair with pile j (where s is 0 or 1). Actually easier: We can think backwards.
But we already did a forward DP that showed for each transition, using leftover when available is optimal. The DP's optimality principle ensures that if we choose c optimally at each step based on current state, the resulting DP[i][r] is maximal. Since the DP recurrence we used (which always uses leftover when s=1 and A_i>0) yields the maximum because we compared both options and found using is never worse, we have proven optimality.
Thus the greedy is correct.
Now, we need to implement. Input format: first line N, then N lines each with A_i. Or possibly they are on one line? The problem statement shows each on separate line. We'll read accordingly.
Edge Cases: N up to 1e5, A_i up to 1e9, so use 64-bit.
I'll write solve() in Python.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
A = [int(next(it)) for _ in range(N)]
rem = 0
total = 0
for a in A:
if rem == 1 and a > 0:
total += 1
a -= 1
rem = 0
total += a // 2
rem = a % 2
print(total)
Testing with examples yields correct.
But wait, check the second example manually:
Input:
8
2
0
1
6
0
8
2
1
Our computed total=9, matches.
Thus solution is straightforward.
But is there any nuance about input order? The input format is N then A_1, A_2, ... A_N each on its own line, but we can just read all ints.
Time complexity O(N). Memory O(1) aside from input array.
Now, double-check for large values: total could be huge, but Python handles.
Now, produce final answer.
But I realize we haven't considered the possibility of pairing across more than one step? No, only at most 1 difference.
Thus final solution.
Now, I'll produce the final response with explanation. We can solve this problem greedily by processing the piles from left to right. At each pile i, we first try to pair a leftover card from the previous pile (if any) with one card from the current pile – this gives one pair. Then we form as many internal pairs as possible within the remaining cards of the pile. The leftover after internal pairing (0 or 1) is passed to the next pile.
This greedy is optimal because it exactly follows the dynamic programming recurrence where the state is just the number of leftover cards (0 or 1) from the previous pile. Using the leftover whenever possible is never worse, and the parity handling ensures the maximum number of pairs.
The algorithm runs in O(N) time and uses O(1) extra space.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
rem = 0 # leftover from the previous pile (0 or 1)
ans = 0
for _ in range(n):
a = int(next(it))
if rem == 1 and a > 0:
ans += 1 # cross pair with previous leftover
a -= 1
rem = 0
ans += a // 2 # internal pairs
rem = a % 2 # leftover for the next pile
print(ans)
if __name__ == "__main__":
solve()