QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#11870. The Bus

統計

The streets of Byte City form a regular, chessboardlike network - they are either north-south or west-east directed. We shall call them NS- and WE-streets. Furthermore, each street crosses the whole city. Every NS-street intersects every WE- one and vice versa. The NS-streets are numbered from $1$ to $n$, starting from the westernmost. The WE-streets are numbered from $1$ to $m$, beginning with the southernmost. Each intersection of the $i$’th NS-street with the $j$'th WE-street is denoted by a pair of numbers $(i,j)$ (for $1 ≤ i ≤ n$, $1 ≤ j ≤ m$).

There is a bus line in Byte City, with intersections serving as bus stops. The bus begins its itinerary by the $(1,1)$ intersection, and finishes by the $(n,m)$ intersection. Moreover, the bus may only travel in the eastern and/or northern direction.

There are passengers awaiting the bus by some of the intersections. The bus driver wants to choose his route in a way that allows him to take as many of them as possible. (We shall make an assumption that the interior of the bus is spacious enough to take all of the awaiting passengers, regardless of the route chosen.)

Write a programme which:

  • reads from the standard input a description of the road network and the number of passengers waiting at each intersection,
  • finds, how many passengers the bus can take at the most,
  • writes the outcome to the standard output.

Input

The first line of the standard input contains three positive integers $n$, $m$ and $k$ - denoting the number of NS-streets, the number of WE-streets and the number of intersections by which the passengers await the bus, respectively ($1 ≤ n ≤ 10^9$, $1 ≤ m ≤ 10^9$, $1 ≤ k ≤ 10^5$).

The following $k$ lines describe the deployment of passengers awaiting the bus, a single line per intersection. In the $(i+1)$’st line there are three positive integers $x_i$, $y_i$ and $p_i$, separated by single spaces, $1 ≤ x_i ≤ n$, $1 ≤ y_i ≤ m$, $1 ≤ p_i ≤ 10^6$. A triplet of this form signifies that by the intersection $(x_i,y_i)$ $p_i$ passengers await the bus. Each intersection is described in the input data once at the most. The total number of passengers waiting for the bus does not exceed $1\,000\,000\,000$.

Output

Your programme should write to the standard output one line containing a single integer - the greatest number of passengers the bus can take.

Example

Input

8 7 11
4 3 4
6 2 4
2 3 2
5 6 1
2 5 2
1 5 5
2 1 1
3 1 1
7 7 1
7 4 2
8 6 2

Output

11
problem_11870_1.gif
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.