Div2 894E — Ralph and Mushrooms

as we know we can find cycle and contract to a point to make the graph become DAG

but tutorial dont explain about finding mushrooms in the cycle by math knowledge

I have read some one's code and the math is as the follow

IF I HAVE MISTAKE WELLCOME TO TELL ME! THX

walk through the cycle me can get the max mushrooms as we could

because we can still pass it though we get 0 mushroom so we can get all the mushrooms

mushrooms decrease by 1,2,3...n

for the initial mushrooms w

i-th time you get w-i(i+1)/2

for every w suppose you can get mushrooms n time

w,w-1,w-1-2,w-1-2-3...w-(1+2..n)

which is w*(n+1)-[i(i+1)/2 i for 0 to n]

-> w(n+1)-i(i+1)(i+2)/6

see here https://en.wikipedia.org/wiki/Tetrahedral_number

consider w is one of the i(i+1)/2

which is what we want,i is the max times to get mushroom

for any w get floor for the ans

n is the max times to get mushroom

int n= floor((sqrt(1+8*w)-1)/2);

sum_mushroom=(n+1)*w — n*(n+1)*(n+2)/6;