Comments

uwu it was fun in moscow, hope we will meet again in the future :)

I considered the input two values at a time. If there are an odd number of integers in initial input we can ignore the last one, because that generates ('s which are not matched later.

Consider a typical parens-matching problem, where you might consider the "depth" of the parens sequence. A open parens ( increases the depth by 1, and a close parens ) decreases the depth by 1. If the next two values in the input are $$$a$$$ ('s and $$$b$$$ )'s, then the closing parens will match $$$b$$$ times. We can simply add these values to our total.

There is a case where having $$$b$$$ )'s will go below the lowest depth reached, and past that point we don't have any ('s to match on the other side. In that case, we can only match up to the lowest level.

The last case to consider is that of valid sequences concatenated together, e.g. ()(()). One observation to make is that each of the valid subsequences (i.e. () and (())) all lie on the same level. Furthermore, we can if we convert this into an input as specified in the problem (1 1 2 2), we can see that these subsequences all end on closing-parens indices.

So the idea is to keep a stack. For each pair of values in the input, take note of the level we end on ($$$cur+a-b$$$). Pop any levels off the stack that are higher than this — once we go down past a level, we can no longer concatenate to create another valid sequence. If the value on the stack is equal to the current level, this means we have a valid sequence to concatenate onto, and we can add it to our total.

There are a few more little edge cases and extra details, but otherwise I hope this gives a good overview of the general idea.

See my solution for reference: 127381648. It's actually quite short for such an annoying problem.

It's really sad that I won't be able to meet up my teammates this summer (nooo Monogon). I took the last week of my internship off to attend this competition, so it's a little frustrating that this notice comes so late, when I'm unable to make many changes to my schedule. Luckily I managed to get credit back for my flights, but I know there are many others who weren't as lucky.