 # Awesome Strings Question - Compressed Bracket Sequence

I need your help in understand the line paragraph of the editorial of https://codeforces.com/problemset/problem/1556/C

The next observation is that if we fix the number of opening brackets taken from 𝑐𝑙cl, then we can count the number of brackets that we should take from 𝑐𝑟cr. Using these observations we can calculate the answer for 𝑙…𝑟l…r using the following formula: 𝑚𝑖𝑛(𝑐𝑙,𝑐𝑟−𝑏𝑎𝑙𝑎𝑛𝑐𝑒)−𝑚𝑎𝑥(1,𝑚𝑖𝑛𝐵𝑎𝑙𝑎𝑛𝑐𝑒)+1min(cl,cr−balance)−max(1,minBalance)+1.

Could someone elaborate this formula possibly provide an example to understand it better?
AC Solution: https://codeforces.com/problemset/status?my=on

In the solution, please explain the left and right equations i.e; what exactly they are calculating

``````static long solve(int []c, int n){
long ans=0;
for(int l=0;l<n;l+=2){
long min_balance=c[l];
long balance=c[l];
for(int r=l+1;r<n;r++){
if(r%2==0){
// adding the open brackets to cur
balance+=c[r];
}else{
//
long left=Math.max(0, balance-c[r]);
long right=Math.min(min_balance, Math.min(balance-1, c[l]-1));

if(left<=right){
ans+=right-left+1;
}
// removing closed brackets from open
balance-=c[r];
min_balance=Math.min(min_balance, balance);
}
}
}
return ans;
}
``````