Editorial for LaserTag


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
EPSILON = 10**-3

def solve(N, R, m, n, xp, targets, reflectors):
    p = (xp, 0)

    left_targets = []
    right_targets = []
    left_reflectors = dict()
    right_reflectors = dict()

    for i in range(N):
        left_targets.append((-targets[i][0], targets[i][1]))
        right_targets.append((2*m - targets[i][0], targets[i][1]))

    for i in range(R):
        if reflectors[i][0] == 0:
            left_reflectors[reflectors[i][1]] = reflectors[i][2]
        elif reflectors[i][0] == m:
            right_reflectors[reflectors[i][1]] = reflectors[i][2]
        else:
            assert False

    status = True
    total_score = 0

    for i in range(N):
        slope1 = (left_targets[i][1] - p[1]) / (left_targets[i][0] - p[0])
        result1 = - slope1 * p[0]
        result1_rounded = round(result1)
        result1 = result1_rounded if abs(result1 - result1_rounded) <= EPSILON else False

        slope2 = (right_targets[i][1] - p[1]) / (right_targets[i][0] - p[0])
        result2 = slope2 * (m - p[0])
        result2_rounded = round(result2)
        result2 = result2_rounded if abs(result2 - result2_rounded) <= EPSILON else False

        if result1 not in left_reflectors and result2 not in right_reflectors:
            status = False
            break
        else:
            score1 = left_reflectors[result1] if result1 in left_reflectors else -1
            score2 = right_reflectors[result2] if result2 in right_reflectors else -1

            assert score1 != -1 or score2 != -1

            total_score += max(score1, score2)

    return total_score if status else -1


if __name__ == '__main__':
    targets = []
    reflectors = []

    N, R, m, n, xp = list(map(int, input().strip().split()))

    for _ in range(N):
        data = tuple(map(int, input().strip().split()))
        assert len(data) == 2
        targets.append(data)

    for _ in range(R):
        data = tuple(map(int, input().strip().split()))
        assert len(data) == 3
        reflectors.append(data)

    print(solve(N, R, m, n, xp, targets, reflectors))