When I first saw this problem, I felt that this problem is somewhat similar to [1068 B. Niko's Tactical Cards](http://mirror.codeforces.com/contest/2173/problem/B) in which DP was used , as I haven't studied DP I was unable to solve it .↵
↵
But After the contest I learn't the basic trick that first we define states , we apply operation in every state and then take min or max as the problem tells and then define the next state for further interations .↵
↵
Now , I applied the same logic during solving the online mirror of 2025-2026 ICPC, NERC, Northern Eurasia Finals [Problem M. Medical Parity](http://mirror.codeforces.com/contest/2181/problem/M) which gave me AC [submission:353928464] .↵
↵
Similarly , I had the same intuition for going for this trick in yesterday's [Good Bye 2025 C problem](http://mirror.codeforces.com/contest/2178/problem/C) as there were two operations held at position 1 and 2 so I tried to define two states . ↵
↵
1st state — it tried to maximise the outcome of the leftmost operation by taking the best X of 1st state so far , then applying a left operation on it and a right operation on it , then reseting it to the best of the left operation of 1st state and best left operation from the 2nd state .↵
↵
2nd state — it tried to maximise the outcome of the rightmost operation by taking the best X of 2st state so far , then applying a left operation on it and a right operation on it , then reseting it to the best of the right operation of 1st state and best right operation from the 2nd state .↵
↵
I did a dry run and it It perfectly worked on pretests 1 but somehow fails on pretests 2 . ↵
Is there a flaw in my logic or did someone implemented a similar approach and got AC . ↵
Can anyone Help ..↵
↵
Submission — [submission:355435711]↵
↵
~~~~~↵
vector<int> a(n) ;↵
for(int i = 0 ; i < n ; i++)cin >> a[i] ;↵
↵
vector<int> l = {0 , 0 , 1} , r = {0 , 0 , 1} ; //l , r = {best X , cur left pointer , cur right pointer}↵
vector<int> ll(3) , lr(3) , rl(3) , rr(3) ;↵
↵
while(l[2] < n)↵
{↵
ll[0] = l[0] + a[l[1]] ; ↵
ll[1] = l[2] ;↵
ll[2] = l[2] + 1 ;↵
↵
lr[0] = l[0] - a[l[2]] ;↵
lr[1] = l[1] ;↵
lr[2] = l[2] + 1 ;↵
↵
rl[0] = r[0] + a[r[1]] ;↵
rl[1] = r[2] ;↵
rl[2] = r[2] + 1 ;↵
↵
rr[0] = r[0] - a[r[2]] ;↵
rr[1] = r[1] ;↵
rr[2] = r[2] + 1 ;↵
↵
if(ll[0] > rl[0])l = ll ;↵
else if(ll[0] == rl[0])↵
{↵
if(a[ll[1]] >= a[rl[1]]) l = ll ;↵
else l = rl ;↵
}↵
else l = rl ;↵
↵
if(lr[0] > rr[0])r = lr ;↵
else if(lr[0] == rr[0])↵
{↵
if(a[lr[1]] >= a[rr[1]])r = lr ;↵
else r = rr ;↵
}↵
else r = rr ;↵
}↵
↵
cout << max(l[0] , r[0]) << endl ;↵
~~~~~↵
↵
Plus , It's my first blog :D
↵
But After the contest I learn't the basic trick that first we define states , we apply operation in every state and then take min or max as the problem tells and then define the next state for further interations .↵
↵
Now , I applied the same logic during solving the online mirror of 2025-2026 ICPC, NERC, Northern Eurasia Finals [Problem M. Medical Parity](http://mirror.codeforces.com/contest/2181/problem/M) which gave me AC [submission:353928464] .↵
↵
Similarly , I had the same intuition for going for this trick in yesterday's [Good Bye 2025 C problem](http://mirror.codeforces.com/contest/2178/problem/C) as there were two operations held at position 1 and 2 so I tried to define two states . ↵
↵
1st state — it tried to maximise the outcome of the leftmost operation by taking the best X of 1st state so far , then applying a left operation on it and a right operation on it , then reseting it to the best of the left operation of 1st state and best left operation from the 2nd state .↵
↵
2nd state — it tried to maximise the outcome of the rightmost operation by taking the best X of 2st state so far , then applying a left operation on it and a right operation on it , then reseting it to the best of the right operation of 1st state and best right operation from the 2nd state .↵
↵
I did a dry run and it It perfectly worked on pretests 1 but somehow fails on pretests 2 . ↵
Is there a flaw in my logic or did someone implemented a similar approach and got AC . ↵
Can anyone Help ..↵
↵
Submission — [submission:355435711]↵
↵
~~~~~↵
vector<int> a(n) ;↵
for(int i = 0 ; i < n ; i++)cin >> a[i] ;↵
↵
vector<int> l = {0 , 0 , 1} , r = {0 , 0 , 1} ; //l , r = {best X , cur left pointer , cur right pointer}↵
vector<int> ll(3) , lr(3) , rl(3) , rr(3) ;↵
↵
while(l[2] < n)↵
{↵
ll[0] = l[0] + a[l[1]] ; ↵
ll[1] = l[2] ;↵
ll[2] = l[2] + 1 ;↵
↵
lr[0] = l[0] - a[l[2]] ;↵
lr[1] = l[1] ;↵
lr[2] = l[2] + 1 ;↵
↵
rl[0] = r[0] + a[r[1]] ;↵
rl[1] = r[2] ;↵
rl[2] = r[2] + 1 ;↵
↵
rr[0] = r[0] - a[r[2]] ;↵
rr[1] = r[1] ;↵
rr[2] = r[2] + 1 ;↵
↵
if(ll[0] > rl[0])l = ll ;↵
else if(ll[0] == rl[0])↵
{↵
if(a[ll[1]] >= a[rl[1]]) l = ll ;↵
else l = rl ;↵
}↵
else l = rl ;↵
↵
if(lr[0] > rr[0])r = lr ;↵
else if(lr[0] == rr[0])↵
{↵
if(a[lr[1]] >= a[rr[1]])r = lr ;↵
else r = rr ;↵
}↵
else r = rr ;↵
}↵
↵
cout << max(l[0] , r[0]) << endl ;↵
~~~~~↵
↵
Plus , It's my first blog :D



