#C657. Minimum Steps to Vacate Train Carriage

    ID: 50344 Type: Default 1000ms 256MiB

Minimum Steps to Vacate Train Carriage

Minimum Steps to Vacate Train Carriage

You are given a train with n rows and m aisle positions per row. There are p passengers located in the train. Each passenger is identified by a pair of integers \( (r, c) \), where \(r\) is the row number and \(c\) is the aisle position in that row.

All the passengers need to vacate the train carriage. The process is such that a passenger in row \(r\) requires exactly \(r\) steps to exit the train. Your task is to determine the minimum number of steps required for all passengers to vacate the carriage. In other words, you need to compute:

[ \text{ans} = \max_{1 \leq i \leq p} (r_i) ]

where \(r_i\) is the row number of the \(i\)-th passenger.

inputFormat

The first line contains three integers \(n\), \(m\), and \(p\) representing the number of rows, number of aisle positions per row, and the number of passengers respectively.

Then \(p\) lines follow, each containing two integers \(r\) and \(c\) — the row number and aisle position of a passenger.

outputFormat

Output a single integer representing the minimum number of steps required for all passengers to vacate the train carriage.

## sample
3 2 5
1 1
1 2
2 1
2 2
3 1
3