Reconstruction of trips based on sensor logs

Kotlin Hard data classes

Task «Reconstruction of trips based on sensor logs»

There are logs from passenger trip sensors: each event is a record of a passenger's entry/exit with a timestamp and trip ID. It is required to restore data for each trip: trip ID, number of passengers (maximum number of people simultaneously in the transport), total number of passengers who made the trip, and trip duration (difference between the last and first event). If a trip lacks events (e.g., only entry or only exit), such a trip is considered invalid and is ignored. All times are given in seconds from the start of the day. Output a list of trips in ascending order of ID.

Input Format

In the first line, an integer N (1 ≤ N ≤ 10^5) — the number of events. Then in N lines: each event in the format <trip_ID> <event_type> <time>. The event type is 'IN' (entry) or 'OUT' (exit). The trip ID is an integer from 1 to 10^9. The time is an integer from 0 to 86400.

Output Format

For each valid trip, output a line: trip_ID max_passenger_count total_passenger_count duration_in_seconds. If there are no valid trips, output 'NO TRIPS'.

Examples

Example 1

INPUT
6 1 IN 100 1 IN 150 1 OUT 200 2 IN 10 2 OUT 20 3 OUT 5
OUTPUT
1 2 2 100 2 1 1 10

Example 2

INPUT
4 1 IN 0 1 OUT 10 2 IN 5 2 OUT 15
OUTPUT
1 1 1 10 2 1 1 10

Example 3

INPUT
10
OUTPUT
NO TRIPS
main.kt