Overflowing Gifts
Time limit1sMemory limit256 MB
Given workers beside a conveyor belt shaped like a reversed C, each with a fixed wrap time, find the maximum number of gifts they can wrap.
- Level
Medium6 of 10
- Topics
- Simulation, Greedy
- Solved
- No attempts yet
Problem
The day after the party, Uk-je found 5,000,500,000,000 gifts that fans had left behind! Moved by this, Uk-je decided to give gifts to the fans too. Too lazy to buy new ones, he only changed the wrapping of the gifts he had received. Still too lazy to do the work himself, he sold some of the gifts and used the money to open a gift wrapping factory.
The grid factory has a ⊐-shaped conveyor belt along its inner edge. The belt starts at the top left cell of the factory and ends at the bottom left cell. Every second, the belt moves one cell along the ⊐ shape toward the end point. All gifts are carried on this belt, and each time the belt moves one cell, one gift is placed at the start point. The belt is empty at the beginning.

Uk-je hires workers to wrap gifts. Workers stand only in cells next to the belt, with at most one person per cell. Worker takes seconds to wrap one gift. Workers are diligent, so whenever they are not wrapping, they check for an adjacent gift. A worker can wrap any gift on the belt in the four directions up, down, left, and right. If two or more gifts are adjacent, the worker wraps the one that has been on the belt longer. Once all gifts have been placed at the start point, no new gift is placed there.
Uk-je is a harsh boss and does not allow workers to rest. So a worker who is not wrapping starts wrapping as soon as the belt moves and a gift arrives in an adjacent cell. A wrapped gift is removed from the belt right away, so the gifts behind it can always pass by freely. When a worker finishes, the worker can immediately wrap another gift. A gift that reaches the end point without being wrapped falls off the belt and is discarded.
Look at the example in the figure. The factory is , there are 6 gifts, and there are 3 workers. Worker 1 is at and takes seconds per gift. Worker 2 is at with . Worker 3 is at with . is the row and column number, counting from . In this case, the workers can wrap 6 gifts in total.
How many gifts can Uk-je deliver to the fans?
Input
The first line contains the factory size (), the number of workers (), and the number of gifts ().
From the second line, the next lines each contain the row (), the column (), and the seconds () for worker .
Output
Print the maximum number of wrapped gifts that Uk-je can obtain.