This page is still under construction.

Parts of this page are still being built. What you see may change.

Evacuation Plan

Time limit1sMemory limit128 MB

Summary
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 (Xi,Yi)(X_i, Y_i) and a shelter at (Pj,Qj)(P_j, Q_j) is

Di,j=∣Xi−Pj∣+∣Yi−Qj∣+1 minutes.D_{i,j} = |X_i - P_j| + |Y_i - Q_j| + 1 \text{ minutes.}

Given the city layout and the Council's plan, decide whether the plan really is optimal.

Input

The first line contains two integers NN and MM separated by a space. NN (1≤N≤1001 \le N \le 100) is the number of municipal buildings, numbered 11 to NN. MM (1≤M≤1001 \le M \le 100) is the number of fallout shelters, numbered 11 to MM.

The next NN lines describe the buildings. Line ii contains three integers XiX_i, YiY_i, and BiB_i, where Xi,YiX_i, Y_i (−1000≤Xi,Yi≤1000-1000 \le X_i, Y_i \le 1000) are the building's coordinates and BiB_i (1≤Bi≤10001 \le B_i \le 1000) is the number of workers in it.

The next MM lines describe the shelters. Line jj contains three integers PjP_j, QjQ_j, and CjC_j, where Pj,QjP_j, Q_j (−1000≤Pj,Qj≤1000-1000 \le P_j, Q_j \le 1000) are the shelter's coordinates and CjC_j (1≤Cj≤10001 \le C_j \le 1000) is its capacity.

The last NN lines describe the Council's plan, one line per building in the same order. Line ii contains MM integers Ei,1,…,Ei,ME_{i,1}, \dots, E_{i,M} (0≤Ei,j≤10000 \le E_{i,j} \le 1000), where Ei,jE_{i,j} is the number of workers evacuating from building ii to shelter jj.

The given plan is guaranteed to be valid: it evacuates exactly BiB_i workers from each building ii 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.

Examples2

  1. Example 1

    Input
    3 4
    -3 3 5
    -2 -2 6
    2 2 5
    -1 1 3
    1 1 4
    -2 -2 7
    0 -1 3
    3 1 1 0
    0 0 6 0
    0 3 0 2
    
    Expected output
    SUBOPTIMAL
    
  2. Example 2

    Input
    3 4
    -3 3 5
    -2 -2 6
    2 2 5
    -1 1 3
    1 1 4
    -2 -2 7
    0 -1 3
    3 0 1 1
    0 0 6 0
    0 4 0 1
    
    Expected output
    OPTIMAL