Balloon Collection

Time limit1sMemory limit128 MB

Summary
Given balloons falling at specific positions and times, find the minimum weighted travel cost for a capacity-3 robot to catch and store all balloons at the origin, or report the first uncatchable balloon.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Simulation
Solved
No attempts yet

Problem

Sanggeun is making a balloon-catching game at TGN. It is a two-dimensional game meant to captivate people's childlike wonder. In the game, balloons fall to the ground one at a time. The player drives a robot car to catch the balloons as they fall. The player can move the robot left or right, or keep it still in place. When a balloon touches the ground, the car must be exactly at that position; if the car is not there, the balloon explodes and the game ends.

The goal of the game is to store every balloon at the house on the left. The robot can carry up to 33 balloons at a time. The robot's movement speed depends on the number of balloons it is carrying: if it is carrying kk balloons (k=0,1,2,3k = 0, 1, 2, 3), moving one cell takes (k+1)(k+1) seconds. The less distance the robot moves, the higher the score the player earns.

Sanggeun wants to compute the highest score a player can achieve. Given the position and the time at which each balloon falls, write a program that finds the minimum number of moves the robot needs to catch every balloon and store it at the house. The robot car starts at the house. If it is impossible to catch every balloon, write a program that finds which balloon is the first one that cannot be caught.

Input

The input consists of several test cases.

The first line of each test case contains the number of balloons nn (0<n≤400 < n \le 40). Each of the next nn lines contains the position pip_i of a balloon and the time tit_i at which the balloon touches the ground, separated by a space (0<pi≤1000 < p_i \le 100, 0<ti≤500000 < t_i \le 50000). For all i<ji < j, ti<tjt_i < t_j. Also, the position of the house is 00, and the game starts at time 00.

The sizes of the robot car, the house, and the balloons are so small that they can be ignored. The time it takes the robot to catch a balloon and the time it takes to store a balloon at the house are both 00. Also, the car starts moving immediately after it receives a command.

The last line of the input contains a single 00.

Output

For each test case, print one of the following two lines.

If the player can catch every balloon, print OK and then the minimum number of moves the robot needs to catch and store all balloons. If the player cannot catch every balloon, print NG and then the number of the first balloon that cannot be caught.

Examples1

  1. Example 1

    Input
    2
    10 100
    100 270
    2
    10 100
    100 280
    3
    100 150
    10 360
    40 450
    3
    100 150
    10 360
    40 440
    2
    100 10
    50 200
    2
    100 100
    50 110
    1
    15 10
    4
    1 10
    2 20
    3 100
    90 200
    0
    
    Expected output
    OK 220
    OK 200
    OK 260
    OK 280
    NG 1
    NG 2
    NG 1
    OK 188