# HALFPIE - Editorial

**URL:** <https://discuss.codechef.com/t/halfpie-editorial/126177>\
**Category:** editorial\
**Tags:** cakewalk, editorial, start265\
**Created:** [November 18, 2026, 4:30pm UTC](https://discuss.codechef.com/t/halfpie-editorial/126177 "2026-11-18T16:30:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [November 18, 2026, 4:30pm UTC](https://discuss.codechef.com/t/halfpie-editorial/126177/1 "2026-11-18T16:30:45Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/HALFPIE)  
[Contest: Division 1](https://www.codechef.com/START265A/problems/HALFPIE)  
[Contest: Division 2](https://www.codechef.com/START265B/problems/HALFPIE)  
[Contest: Division 3](https://www.codechef.com/START265C/problems/HALFPIE)  
[Contest: Division 4](https://www.codechef.com/START265D/problems/HALFPIE)

_ **Author:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

Cakewalk

# PREREQUISITES:

None

# PROBLEM:

Chef sells one pie on each of N days, at price A\_i on day i (all prices are even).  
He must halve the price on exactly two consecutive days.  
Find the maximum total amount he can earn.

# EXPLANATION:

Without any discount, Chef would earn A\_1 + A\_2 + \ldots + A\_N.

If the discount is applied on days i and i+1, Chef loses exactly half of each of those prices, i.e. \frac{A\_i + A\_{i+1}}{2}.

So, to maximize the earnings, we want to minimize this loss.  
There are only N-1 choices of i, so just try all of them!

The answer is hence the maximum value of

\text{sum}(A) - \frac{A\_i + A\_{i+1}}{2}

across all 1 \le i \lt N.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n = int(input())
> a = list(map(int, input().split()))
>     
> s = sum(a)
> ans = 0
> for i in range(n-1):
> ans = max(ans, s - (a[i]+a[i+1])//2)
> print(ans)
> 
> ```
