The Bus
Time limit3sMemory limit512 MB
Find a path from (1,1) to (n,m) moving only east and north that collects the maximum total of passenger weights at visited intersections.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Sorting, Binary search
- Solved
- No attempts yet
Problem
The streets of a city form a regular, chessboard-like grid: every street runs either north-south (an NS-street) or west-east (a WE-street), and each street spans the whole city. Every NS-street intersects every WE-street and vice versa. The NS-streets are numbered from to starting from the westernmost, and the WE-streets are numbered from to starting from the southernmost. The intersection of the -th NS-street with the -th WE-street is denoted by the pair (for , ).
A bus line uses these intersections as stops. The bus starts at intersection and finishes at intersection , and it may move only east and/or north (that is, each move increases the NS-street number or the WE-street number).
Passengers wait at some of the intersections. The driver wants to choose a route that picks up as many passengers as possible (assume the bus is spacious enough to carry everyone it passes, regardless of the route chosen).
Write a program that reads a description of the road network and the number of passengers waiting at each intersection, and outputs the greatest number of passengers the bus can pick up.
Input
The first line contains three positive integers , and - the number of NS-streets, the number of WE-streets, and the number of intersections at which passengers are waiting (, , ).
Each of the next lines describes one intersection with three space-separated positive integers , and (, , ), meaning that passengers wait at intersection . Each intersection appears at most once in the input. The total number of waiting passengers does not exceed .
Output
Output a single line with one integer: the greatest number of passengers the bus can pick up.
Hint
