Server Room Safety

Author: Nazmul Alam

You are appointed as a system admin in a firm, and it is your responsibility to check the safety of the server racks in one of the server rooms. The server racks are arranged in an array. If any rack reaches another rack when it falls, it will cause the other rack to fall. Safety rules require that at least some of the racks should remain standing if one of the ends falls toward the others.

Your task is to determine whether either or both ends will cause all racks to fall should one be tipped. To determine if a rack will knock over another rack, use the following two rules*:

Left rack falls:

    1. Left rack falls: position[i] + h[i] >= position[i_to_test]
    2. Right rack falls: position[ i ] - h[ i ] <= position[ i_to_test ]

    * Assume the position of a rack is position[ i ] and its height is h[ i ] .

    The test is based on a single event. In other words, if it takes toppling both the left and right ends to knock down all the servers, you still pass the safety inspection.

    An example follows:

    In the image shown below, rack R1 can trigger the fall of rack R2 since  1+3 >= 4 , but rack can't trigger the fall of rack since 4 - 2 &#8816; 1

    Input Format

    In the first line, the number of racks, , will be given.

    In the second line, the position of each rack, , will be given.

    In the third line, the height of each rack, , will be given.

    Constraints

    0 <= n <= 100

    0 <= xi <= 1000

    Output Format

    If the racks fall over no matter from which end it gets triggered, then return BOTH. If the racks fall over due to rack at leftmost or rightmost, then return LEFT or RIGHT respectively. If neither end toppling will cause all the racks to fall over, return NONE.

    Samples

    Input
    5 1 2 3 4 5 1 1 1 1 1
    Output
    BOTH
    Limits
    Language Time Memory
    GNU C 11 1s 512MB
    GNU C++ 14 1s 512MB
    GNU C++ 11 1s 512MB
    PHP 7 1s 1024MB
    Java (OpenJDK 8) 1s 4096MB
    Statistics
    Login To Submit