Not able to solve this

I am trying to solve this question…
https://codeforces.com/contest/1323/problem/C
I tried for a long time…But couldn’t reach to a proper acceptable solution.
I saw the editorial… But not able to get much intution.
Please tell me… what should be the approach in this question.

hello @ashishnnnnn

have u solved balanced paranthesis problem?

Yes…There we use stack…
Actually i have used something like that…
In this question…
https://codeforces.com/contest/1323/submission/99316928
Couldn’t find where i am wrong.

if string containts only ( and ) then the idea without stack is that we iterate from left to right if we encounter ( then we incremnt the count and i we encounter ) then we decrement the count[ removing 1 -> ( from the count ] at the end if the value of count is zero then that indicates that string is balanced otherwise not balanced

let me know r u getting this or not

Yaa… But here we don’t have to check only that whether string is balanced or not…

i m talking about idea not the solution

Yaa… I got the idea to check if balanced or not…
similar i have done in my subssion… There i have counted number of “(” and number of “)”…
If they are equal…means it can be made as balanced string.

ok ,another propery that balanced paranthesis satisfies is number of for and index i number of opening brackets till i >= number of closing brackets till i.

let me know it make sense or not

Yaa… Make sense…

that means for balanced paranthesis count will nver be negative right?
it will be either be + ve (number of opening brackets > number of closing brackets) or 0 (balanced) right?

Yaa…got till here…

image
now the first graph should make sense to u
just plotting count on y axis and index on x axis.

Yaa… Make sense… :slight_smile:

now the mountains that are down the line must be above the line to make the overall string balanced.
becuase just now we discussed that count for balnaced paranthsis cant be negative

Yaa… that is right…

so the problem is solved now. we just need to pick such negative mountains and make them positive/0.
so answer will be summation of lenght of all negative mountains

Yaa…means mountains which are negative we have to make them positive…

yes… . … . . .
. . .

Yaa… Got it…
Thanks … A lot of thanks for explaining with that much detail… … :slight_smile::slight_smile:

keep this technique in mind. this is very much helpful in solving parathesis based problems