Populate forces going from right to left
f = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' L ' ) { f = N ; } else if ( S [ i ] == ' R ' ) { f = 0 ; } else { f = max ( f - 1 , 0 ) ; } force [ i ] -= f ; }