I participated in Good Bye 2025 and solved only Problem A.
I spent most of my time on Problem C ("First or Second"). I tried many approaches:
- Recursive solution — simulated removing first or second child.
- Added memoization with
str(arr)as key. - Got MLE ("Need a bigger sleigh") on test 3.
Recursion solution :
def maximum_earn(arr):
if len(arr) == 1:
return 0
op1 = arr[0] + maximum_earn(arr[1:]) # remove first
op2 = -arr[1] + maximum_earn([arr[0]] + arr[2:]) # remove second
return max(op1, op2)
Recursion + memoization :
memo = {}
def maximum_earn(arr):
if len(arr) == 1:
return 0
key = str(arr)
if key in memo:
return memo[key]
op1 = arr[0] + maximum_earn(arr[1:])
op2 = -arr[1] + maximum_earn([arr[0]] + arr[2:])
memo[key] = max(op1, op2)
return memo[key]
I then tried a greedy idea: choose which child remains, and compute the best $$$X$$$.
My first greedy formula:
If child $$$i$$$ remains, $$$X = \text{sum}(a[0:i]) - \text{sum}(a[i+1:])$$$
It worked on small cases but failed on [7, -6, -1, -8, -8] (got 24, expected 29).
I realized:
- For elements before the survivor, I can sometimes choose signs (e.g., subtract a negative to add value).
- So I changed the left part to sum(abs(x) for x in arr[:i]).
This is my final code (O(n²) — works on samples but TLE on large inputs):
import sys
def maximum_earn(arr):
n = len(arr)
maxx = float('-inf')
for i in range(n):
# Left part: you can choose signs → use absolute values
left_sum = sum(abs(x) for x in arr[:i])
# Right part: you must subtract → contribution = -sum(arr[i+1:])
right_sum = sum(arr[i+1:])
current = left_sum - right_sum
if current > maxx:
maxx = current
return maxx
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
print(maximum_earn(a))
It gives 29 for the last sample, but TLE on large inputs due to O(n²).









I also used dp you can check my solution. 355361808
I have seen the solution. You have used 2D dp. Am I right ?
use prefix sums
Auto comment: topic has been updated by anikchand461 (previous revision, new revision, compare).
Auto comment: topic has been updated by anikchand461 (previous revision, new revision, compare).
exactly same
yes
Why throw away the value you already computed for the sums? the difference is just the value (or absolute value) of a[i].
Also the first element in the array can never be taken negative because it can never reach the second position.
I have taken -inf so that anything comes it can change the value
Can anyone telll me why my blog post is getting downvotes ?