Evacuation Plan
Time limit1sMemory limit128 MB
Given buildings with worker counts, shelters with capacities, and a valid assignment plan, decide whether the plan minimizes total Manhattan-plus-one travel time over all valid plans.
- Level
Hard8 of 10
- Topics
- Graph, Minimum spanning tree, Shortest path, Greedy
- Solved
- No attempts yet
Problem

The City has several municipal buildings and several fallout shelters, built to protect municipal workers in case of a nuclear war. Each shelter can hold only a limited number of people, and across the whole city there is almost no spare capacity. If every worker simply ran to the nearest shelter, some shelters would overflow while others sat half-empty.
To avoid this, the City Council prepared an evacuation plan. Instead of assigning each worker to a shelter individually, the plan states, for every municipal building, how many of its workers go to each shelter. A plan is valid when:
- every worker of every building is assigned to some shelter, and
- no shelter is assigned more workers than its capacity (a shelter may be left partly empty).
The City Council claims their plan is optimal: among all valid plans it minimizes the total time to reach the shelters, defined as the sum over all workers of the travel time from each worker's building to the shelter assigned to that worker.
The City is a rectangular grid. The travel time between a municipal building at and a shelter at is
Given the city layout and the Council's plan, decide whether the plan really is optimal.
Input
The first line contains two integers and separated by a space. () is the number of municipal buildings, numbered to . () is the number of fallout shelters, numbered to .
The next lines describe the buildings. Line contains three integers , , and , where () are the building's coordinates and () is the number of workers in it.
The next lines describe the shelters. Line contains three integers , , and , where () are the shelter's coordinates and () is its capacity.
The last lines describe the Council's plan, one line per building in the same order. Line contains integers (), where is the number of workers evacuating from building to shelter .
The given plan is guaranteed to be valid: it evacuates exactly workers from each building and never exceeds any shelter's capacity.
Output
Print OPTIMAL if the Council's plan minimizes the total time to reach the shelters. Otherwise print SUBOPTIMAL, meaning some valid plan achieves a strictly smaller total time.