Java TLE With CodeForces Garland Problem

Hi all,

I’ve been attempting the CF problem “An impassioned circulation of affection”. I came up with a solution in Java, but it gives me TLE. I thought the time complexity of my program was O(n*q), which I thought should pass the constraints of the problem, but it gives me TLE. Therefore, I was wondering what I was doing wrong. Here is the link to my code:

I appreciate all help. Thanks!

I’m not 100% sure, but the constant factor of Java code might be the thing that results in TLE. On the other hand, your 2P implementation might be wrong (I haven’t really looked at it closely).