Problem:
Problem B:
Balloon Collecting
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to move left or right, or simply stay. When one of the balloons reaches the ground, the vehicle and the balloon must reside at the same position, otherwise the balloon will burst and the game ends.
Figure B.1: Robot vehicle and falling balloons
The goal of the game is to store all the balloons into the house at the left end on the game field. The vehicle can carry at most three balloons at a time, but its speed changes according to the number of the carrying balloons. When the vehicle carries
k
balloons (
k
= 0, 1, 2, 3), it takes
k
+1 units of time to move one unit distance. The player will get higher score when the total moving distance of the vehicle is shorter.
Your mission is to help the game designer check game data consisting of a set of balloons. Given a landing position (as the distance from the house) and a landing time of each balloon, you must judge whether a player can capture all the balloons, and answer the minimum moving distance needed to capture and store all the balloons. The vehicle starts from the house. If the player cannot capture all the balloons, you must identify the first balloon that the player cannot capture.
Input
The input is a sequence of datasets. Each dataset is formatted as follows.
n
p
1
t
1
.
.
.
p
n
t
n
The first line contains an integer n, which represents the number of balloons (0 <
n
≤ 40). Each of the following
n
lines contains two integers
p
i
and
t
i
(1 ≤
i
≤
n
) separated by a space.
p
i
and
t
i
represent the position and the time when the
i
-th balloon reaches the ground (0 <
p
i
≤ 100, 0 <
t
i
≤ 50000). You can assume
t
i
<
t
j
for
i
<
j
. The position of the house is 0, and the game starts from the time 0.
The sizes of the vehicle, the house, and the balloons are small enough, and should be ignored. The vehicle needs 0 time for catching the balloons or storing them into the house. The vehicle can start moving immediately after these operations.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output one word and one integer in a line separated by a space. No extra characters should occur in the output.
If the player can capture all the balloons, output "OK" and an integer that represents the minimum moving distance of the vehicle to capture and store all the balloons.
If it is impossible for the player to capture all the balloons, output "NG" and an integer
k
such that the
k
-th balloon in the dataset is the first balloon that the player cannot capture.
Sample 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
Output for
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

array<array<array<int,4>,101>,50501> dp;

int main(){
    int n, INF = 1 << 30, r = 50500;
    while(cin >> n, n){
        int ans = -1;
        for(int t = 0; t <= r; t++){
            for(int p = 0; p <= 100; p++){
                for(int j = 0; j < 4; j++){
                    dp[t][p][j] = INF;
                }
            }
        }
        vector<array<int,2>> bl(n);
        for(int i = 0; i < n; i++){
            cin >> bl[i][0] >> bl[i][1];
        }
        bool flag = true;
        dp[0][0][0] = 0;
        for(int t = 0, bi = 0; t < r; t++){
            if(bi < n && t == bl[bi][1]){
                int pos = bl[bi][0];
                for(int nt = t; nt <= t + 3 && nt <= r; nt++){
                    for(int p = 0; p <= 100; p++){
                        if(nt == t && p == pos)continue;
                        for(int j = 0; j < 4; j++){
                            dp[nt][p][j] = INF;
                        }
                    }
                }
                for(int j = 2; j >= 0; j--)dp[t][pos][j+1] = dp[t][pos][j];
                dp[t][pos][0] = INF;
                int minv = *min_element(dp[t][pos].begin(), dp[t][pos].end());
                if(minv == INF){
                    flag = false;
                    cout << "NG " << bi + 1 << '\n';
                    break;
                }
                bi++;
            }
            dp[t][0][0] = *min_element(dp[t][0].begin(), dp[t][0].end());
            for(int p = 0; p <= 100; p++){
                for(int j = 0; j <= 3 && t + j + 1 <= r; j++){
                    dp[t+1][p][j] = min(dp[t+1][p][j], dp[t][p][j]);
                    if(p >= 1){
                        dp[t+j+1][p-1][j] = min(dp[t+j+1][p-1][j], dp[t][p][j] + 1);
                    }
                    if(p + 1 <= 100){
                        dp[t+j+1][p+1][j] = min(dp[t+j+1][p+1][j], dp[t][p][j] + 1);
                    }
                }
            }
        }
        if(flag){
            cout << "OK " << *min_element(dp[r][0].begin(), dp[r][0].end()) << '\n';
        }
    }
}
accuracy: 1
time_ms: 1380
memory_kb: 83024
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 100000001



int main(){

	while(true){
		int n;
		cin>>n;
		if(n==0)break;
		vector<int> p(n+1,0),t(n+1,0);
		for(int i=1;i<=n;i++)cin>>p[i]>>t[i];
		
		vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(4,vector<int>(10000,Inf)));
		dp[0][0][0] = 0;
		string S = "";
		int ans = -1;
		for(int i=0;i<n;i++){
			int T = t[i+1] - t[i];
			int P = abs(p[i+1] - p[i]);
			for(int j=0;j<=3;j++){
				for(int x=0;x<10000;x++){
					if(dp[i][j][x]==Inf)continue;
					int k = (j+1);
					if(x+p[i]+p[i+1]<10000)dp[i+1][1][x+p[i]+p[i+1]] = min(dp[i+1][1][x+p[i]+p[i+1]],dp[i][j][x]+p[i]*k + p[i+1]);
					if(j!=3){
						if(x+P<10000)dp[i+1][j+1][x+P] = min(dp[i+1][j+1][x+P],dp[i][j][x]+P*k);
					}
				}
			}
			S = "NG";
			for(int j=0;j<=3;j++){
				for(int k=0;k<10000;k++){
					if(dp[i+1][j][k]<=t[i+1]){
						S = "";
						dp[i+1][j][k] = t[i+1];
					}
					else dp[i+1][j][k] = Inf;
				}
			}
			if(S=="NG"){
				ans = i+1;
				break;
			}
		}
		if(S!="NG"){
			S = "OK";
			ans = Inf;
			for(int i=0;i<10000;i++){
				for(int j=0;j<=3;j++){
					if(dp.back()[j][i] <= t.back()){
						ans = min(ans,i + p.back());
					}
				}
			}
		}
		cout<<S<<' '<<ans<<endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 9608
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>

using namespace std;
using ll = long long;

#define INF 1e9

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int n;
  while(cin >> n && n){
    vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(50001, vector<int>(4, INF)));
    vector<int> point(n+1);
    vector<int> times(n+1);
    point[0] = 0;
    times[0] = 0;
    dp[0][0][0] = 0;
    int i, p, t;
    bool flag;
    for(i = 0; i < n; i++){
      cin >> p >> t;
      point[i+1] = p;
      times[i+1] = t;
      flag = false;
      for(int j = 0; j < 4; j++){
        if(times[i] + point[i]*(j+1) + p <= t && dp[i][times[i]][j] != INF){
          flag = true;
          dp[i+1][t][1] = min(dp[i+1][t][1], dp[i][times[i]][j] + point[i] + p);
        }
      }
      for(int j = 0; j < 3; j++){
        if(times[i] + abs(p - point[i]) * (j+1) <= t && dp[i][times[i]][j] != INF){
          flag = true;
          dp[i+1][t][j+1] = min(dp[i+1][t][j+1], dp[i][times[i]][j] + abs(p - point[i]));
        }
      }
      if(!flag) break;
    }

    if(!flag){
      i++;
      cout << "NG" << ' ' << i << endl;
      for(i; i < n; i++) cin >> p >> t;
    }
    else{
      int ans = dp[n][times[n]][0];
      for(i = 1; i < 4; i++) ans = min(ans, dp[n][times[n]][i] + point[n]);
      cout << "OK" << ' ' << ans << endl;
    }
  }
  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}
accuracy: 1
time_ms: 2300
memory_kb: 117760
final_rank: 19

code_snippet: #define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

int dp[4][101][51001];

void solve(){
	while (true) {
		int n;
		cin >> n;
		if (n == 0) return;
		vector<pair<int, int>> inputs;
		REP(i, n) {
			int a, b;
			cin >> a >> b;
			inputs.push_back(mp(a, b));
		}
		inputs.push_back(mp(0, 1e9));
		reverse(ALL(inputs));
		REP(t, 4) {
			REP(i, 101) {
				REP(q, 51001) {
					dp[t][i][q] = 1e9;
				}
			}
		}
		dp[0][0][0] = 0;
		int ans = -1;
		int tmp = 1;
		for (int i = 0; i <= 50500; ++i) {
			if (inputs.back().second == i) {
				for (int t = 1; t <= 3; ++t) {
					REP(q, 101) {
						REP(j, 4) {
							dp[j][q][i + t] = 1e9;
						}
					}
				}
				REP(q, 101) {
					REP(j, 4) {
						if (q != inputs.back().first) {
							dp[j][q][i] = 1e9;
						}
					}
				}
				for (int j = 3; j >= 1;--j) {
					swap(dp[j][inputs.back().first][i], dp[j - 1][inputs.back().first][i]);
				}
				dp[0][inputs.back().first][i] = 1e9;
				int ok = 0;
				REP(j, 4) {
					if (dp[j][inputs.back().first][i] != 1e9) ok = 1;
				}
				
				if (ok == 0) {
					ans = tmp;
					break;
				}
				tmp++;
				inputs.pop_back();
			}
			REP(t, 4) {
				for (int q = 0; q < 101; ++q) {
					if (q != 100) {
						dp[t][q + 1][i + t + 1] = min(dp[t][q + 1][i + t + 1], dp[t][q][i] + 1);
					}
					if (q != 0) {
						if(q == 1){
							dp[0][q - 1][i + t + 1] = min(dp[0][q - 1][i + t + 1], dp[t][q][i] + 1);
						}
						dp[t][q - 1][i + t + 1] = min(dp[t][q - 1][i + t + 1], dp[t][q][i] + 1);
					}
					dp[t][q][i + 1] = min(dp[t][q][i + 1], dp[t][q][i]);
				}
			}
		}
		if (ans != -1) {
			cout << "NG " << ans << endl;
			continue;
		}
		cout << "OK " << dp[0][0][50500] << endl;
	}
}

#undef int
int main() {
	init();
	solve();
}
accuracy: 1
time_ms: 2130
memory_kb: 83632
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int dp[41][60001][3] = {};
int main(){
    int N;
    int inf = 1e5;
    while(cin >> N && N){
        vector<int> P(N+1,0),T(N+1,0);
        for(int i=0;i<=N;i++) for(int j=0;j<=60000;j++) for(int k=0;k<=2;k++) dp[i][j][k] = inf;
        for(int i=1;i<=N;i++) cin >> P[i] >> T[i];
        dp[0][0][0] = 0;
        for(int i=1;i<=N;i++){
            for(int j=0;j<=T[i];j++){
                if(j+P[i]<=T[i]){
                    dp[i][T[i]+2*P[i]][0] = min(dp[i][T[i]+2*P[i]][0],dp[i-1][j][0]+2*P[i]);
                    dp[i][T[i]][1] = min(dp[i][T[i]][1],dp[i-1][j][0]+P[i]);
                }
                for(int k=1;k<=2;k++){
                    if(j+(k+1)*abs(P[i]-P[i-1])<=T[i]){
                        dp[i][T[i]+(k+2)*P[i]][0] = min(dp[i][T[i]+(k+2)*P[i]][0],dp[i-1][j][k]+abs(P[i]-P[i-1])+P[i]);
                        if(k==1) dp[i][T[i]][k+1] = min(dp[i][T[i]][k+1],dp[i-1][j][k]+abs(P[i]-P[i-1]));
                    }
                }
            }
        }
        int ans = inf;
        for(int j=0;j<=60000;j++) ans = min(ans,dp[N][j][0]);
        if(ans!=inf){
            cout << "OK " << ans << endl;
            continue;
        }
        cout << "NG ";
        for(int i=1;i<=N;i++){
            bool ok = true;
            for(int j=0;j<=60000;j++) if(dp[i][j][0]!=inf) ok = false;
            if(ok){
                cout << i << endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 180
memory_kb: 31904
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, dp[2][2][3][8001];

void update(int& a, int t) {
	if (a == -1) a = t;
	else a = min(a, t);
}

void solve(int n) {
	memset(dp, -1, sizeof(dp));
	dp[0][0][0][0] = 0;
	
	int prv = 0, ng_i = -1;
	for (int i=0; i<n; ++i) {
		int p, t;
		cin >> p >> t;
		
		memset(dp[1], -1, sizeof(dp[1]));
		
		bool ng = true;
		for (int j=0; j<2; ++j) for (int k=0; k<3; ++k) for (int d=0; d<8001; ++d) {
			int cur_t = dp[0][j][k][d];
			if (cur_t == -1) continue;
			
			int nxt_t = cur_t + (k+1) * abs(prv * j - p);
			if (nxt_t > t) continue;
			
			int nxt_d = d + abs(prv * j - p);
			if (k + 1 < 3) update(dp[1][1][k+1][nxt_d], t);

			int back_t = t + (k+2) * p, back_d = nxt_d + p;
			update(dp[1][0][0][back_d], back_t);
			
			ng = false;
		}
		
		prv = p;
		swap(dp[0], dp[1]);
		
		if (ng && ng_i == -1) ng_i = i+1;
	}
	
	if (ng_i == -1) {
		int ans = -1;
		for (int d=8000; d>=0; --d) if (dp[0][0][0][d] != -1) ans = d;
		cout << "OK " << ans << endl;
	}
	else {
		cout << "NG " << ng_i << endl;
	}
}

int main() {
	while (cin >> n, n) {
		solve(n);
	}
}
accuracy: 1
time_ms: 40
memory_kb: 3400
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

// よこ, たて
vector<pair<int, int>> v;
int ok=100000000;
int ng=-1;
void msearch(int i, int p, int bn, int t, int x, int move){
    if(move<ok){
        if(i==v.size()){
            if(ok>move+x){
                ok=move+x;
            }
        }else if(x==0){
            if(t+v[i].first>v[i].second){
                if(ng<p){
                    ng=p;
                }
            }else{
                t=v[i].second;
                x=v[i].first;
                move+=v[i].first;
                msearch(i+1, p+1, bn+1, t, x, move);
            }
        }else{
            //cout<<"x: "<<x<<" bn: "<<bn<<" vfront: "<<v[i].first<<" "<<v[i].second<<" t: "<<t<<endl;
            if(abs(x-v[i].first)*(bn+1)+t<=v[i].second && bn<3){
                msearch(i+1, p+1, bn+1, v[i].second, v[i].first, move+abs(x-v[i].first));
            }
            if(t+x*(bn+1)+v[i].first>v[i].second){
                if(ng<p){
                    ng=p;
                }
            }else{
                msearch(i+1, p+1, 1, v[i].second, v[i].first, move+x+v[i].first);
            }
        }
    }
}

int main(void){
    // Your code here!
    int n;
    while(cin>>n,n){
        v.resize(n);
        for(auto& a:v){
            cin>>a.first>>a.second;
        }
        msearch(0, 1, 0, 0, 0, (unsigned long long)0);
        if(ok==100000000){
            cout<<"NG "<<ng<<endl;
        }else{
            cout<<"OK "<<ok<<endl;
        }
        v.clear();
        ok=100000000;
        ng=-1;
    }
}
accuracy: 1
time_ms: 410
memory_kb: 2992
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define INF 1e9
#define EPS 1e-9
#define MOD (1e9+7)
void put(string d){}template<class H,class...T>void put(string d,H&h,T&...t){cout<<h;if(sizeof...(t))cout<<d;put(d,t...);}
template<class T>void puti(T&a,string d=" "){bool f=1;for(auto&_:a)cout<<(exchange(f,0)?"":d)<<_;cout<<endl;}
template<class T>void putii(T&a,string d=" "){for(auto&_:a)puti(_,d);}

vector<pii> b;
using data=tuple<int,int,int,int,int>;
map<data,pair<bool,int>> memo;

pair<bool,int> solve(int pos, int index, int lefttime, int carry, int totalDist){
	data d=make_tuple(pos, index, lefttime, carry, totalDist);
	if(memo.find(d)!=memo.end()) return memo[d];
	//cout<<pos<<" "<<index<<" "<<lefttime<<" "<<carry<<" "<<totalDist<<endl;
	if(index==b.size()) return memo[d]=make_pair(true, totalDist+pos);
	int bpos=b[index].first;
	int bhigh=b[index].second-lefttime;
	int dist=abs(pos-bpos);
	int need=dist*(carry+1);
	if(bhigh<need){
		return memo[d]=make_pair(false,index+1);	//目標を取れない
	}else{
		lefttime+=bhigh;
	}
	if(carry==2){	//3つめを持った
		return memo[d]=solve(0, index+1,lefttime+bpos*(carry+2), 0, totalDist+dist+bpos);
	}
	auto ans_next=solve(bpos, index+1, lefttime, carry+1, totalDist+dist);
	need=bpos*(carry+2);
	auto ans_home=solve(0, index+1,lefttime+need, 0, totalDist+dist+bpos);
	if(ans_home.first==false and ans_next.first==false) {
		if(ans_home.second>ans_next.second){
			return memo[d]=ans_home;
		}else{
			return memo[d]=ans_next;			
		}
	}
	if(ans_home.first==false) return memo[d]=ans_next;
	if(ans_next.first==false) return memo[d]=ans_home;
	if(ans_next.second > ans_home.second) return memo[d]=ans_home;
	else return memo[d]=ans_next;
}


int main(){
	int n;
	while(cin>>n,n){
		memo.clear();
		b.resize(n);
		for(auto&i:b)cin>>i.first>>i.second;
		auto res=solve(0,0,0,0,0);
		cout<<(res.first?"OK ":"NG ")<<res.second<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 7436
final_rank: 4

code_snippet: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

int g_dis;
int ng;
int n;
pair<int, int> p[50]; //???????????????

void dfs(int i, int d, int k){
    if(k > 3) return;
//    show(t)
//        show(i)
    int c = i == 0 ? 0 : p[i - 1].first;
    int t = i == 0 ? 0 : p[i - 1].second;
    if(g_dis < d) return;
    if(i == n){
        g_dis = min(g_dis, d + c);
        return;
    }

    //show(p[i].second)
    //show(dist * (k + 1))
    //show(c * (k + 1) + p[i].first)

    int dist = abs(p[i].first - c);
    ng = max(ng, i);
    if(p[i].second >= t + dist * (k + 1)) dfs(i + 1, d + dist, k + 1);
    if(p[i].second >= t + c * (k + 1) + p[i].first) dfs(i + 1, d + c + p[i].first, 1);
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(cin >> n,n){
        g_dis = 1e8;
        ng = 0;
        rep(i,n) cin >> p[i].first >> p[i].second;
        dfs(0,0,0);
        if(g_dis == 1e8) cout << "NG " << ng + 1 << endl;
        else cout << "OK " << g_dis << endl;
    }
}
accuracy: 1
time_ms: 950
memory_kb: 3100
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

const int INF=123456789;

int dp[5][4][101];
int nx[5][4][101];
int b[50500];

inline void init()
{
    memset(b,-1,sizeof(b));
    rep(i,5)rep(j,4)rep(k,101) dp[i][j][k]=INF;
}

int main()
{
    int n;
    while(cin >>n,n)
    {
        init();

        map<int,int> num;
        vector<int> P(n), T(n);
        rep(i,n)
        {
            scanf(" %d %d", &P[i], &T[i]);
            b[T[i]]=P[i];
            num[T[i]]=i;
        }

        int ans=-1;
        bool fail=false;
        dp[1][0][0]=0;
        for(int t=1; t<50500; ++t)
        {
            rep(w,4)
            {
                if(b[t]==-1)
                {
                    bool ok=(t-(w+1)>=0);
                    if(ok)
                    {
                        for(int i=t; i>t-(w+1); --i) if(b[i]!=-1) ok=false;
                    }

                    if(ok) dp[0][0][0]=min(dp[0][0][0],dp[w+1][w][1]+1);

                    for(int pos=1; pos<=100; ++pos)
                    {
                        dp[0][w][pos]=min(dp[0][w][pos],dp[1][w][pos]);
                        if(ok)
                        {
                            dp[0][w][pos]=min(dp[0][w][pos],dp[w+1][w][pos-1]+1);
                            if(pos+1<=100) dp[0][w][pos]=min(dp[0][w][pos],dp[w+1][w][pos+1]+1);
                        }
                    }
                }
                else
                {
                    int b_idx=num[t];
                    int pos=b[t];
                    if(w<3)
                    {
                        dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[1][w][pos]);

                        if( t-(w+1)>=0 && (b_idx==0 ||(b_idx>0 && t-(w+1)>=T[b_idx-1])) )
                        {
                            if(pos-1>=0) dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[w+1][w][pos-1]+1);
                            if(pos+1<=100) dp[0][w+1][pos]=min(dp[0][w+1][pos],dp[w+1][w][pos+1]+1);
                        }
                    }
                }
            }

            if(b[t]!=-1)
            {
                // printf("  %d th balloon, (%d,%d)\n", num[t],P[num[t]],T[num[t]]);
                bool c=false;
                rep(j,4)
                {
                    // printf(" %d, %d\n",j,dp[0][j][b[t]] );
                    if(dp[0][j][b[t]]<INF) c=true;
                }
                if(!c)
                {
                    fail=true;
                    ans=num[t]+1;
                    break;
                }
            }

            for(int i=4; i>0; --i)rep(j,4)rep(k,101) dp[i][j][k]=dp[i-1][j][k];
            rep(i,4)rep(j,101) dp[0][i][j]=INF;
        }

        if(fail) printf("NG %d\n", ans);
        else printf("OK %d\n", dp[1][0][0]);
    }
    return 0;
}
accuracy: 1
time_ms: 2120
memory_kb: 3276
final_rank: 16

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)

int main(){
	int n;
	while(cin>>n&&n){
		static int dp[50][5][10000];
		rep(i,50)rep(j,5)rep(k,10000)dp[i][j][k] = INF;
		dp[0][0][0] = 0;
		
		int bef = 0;
		bool endf = false;
		int p,t;
		rep(i,n){
			cin>>p>>t;
			if(endf)continue;
			rep(j,3){
				rep(k,10000){
					if(k+abs(p-bef)>=10000)continue;
					int res = dp[i][j][k] + (j+1)*abs(p-bef);
					if(res<=t)
						dp[i+1][j+1][k+abs(p-bef)] = t;
				}
			}
			for(int j=1;j<=3;j++){
				rep(k,10000){
					if(k+bef+p>=10000)continue;
					int res = dp[i][j][k]+(j+1)*bef+p;
					if(res<=t)
						dp[i+1][1][k+bef+p] = t;
				}
			}
			
			bool f=false;
			rep(j,4){
				rep(k,10000){
					if(dp[i+1][j][k]<=t)f=true;
				}
			}
			if(!f){
				cout<<"NG "<<i+1<<endl;
				endf = true;
			}
			
			bef = p;
		}
		if(!endf){
			int ans = INF;
			rep(i,4){
				rep(j,10000){
					if(dp[n][i][j]<=t){
						ans = min<ll>(ans,j+p);
					}
				}
			}
			cout<<"OK "<<ans<<endl;
		}
	}
	
}
accuracy: 1
time_ms: 90
memory_kb: 12780
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int n,dis[41],t[41],maxball,ans;

void saiki(int x,int now,int ball,int sum,int kai){
  if(ans<sum)return;
  maxball=max(maxball,kai);
  if(kai==n&&!ball)ans=min(ans,sum);
  if(ball)saiki(0,now+x*(1+ball),0,sum+x,kai);
  if(ball==3 || kai==n) return;
  
  int nx=dis[kai];
  int nsum=sum+abs(x-dis[kai]);
  int nnow=now+abs(x-dis[kai])*(1+ball);
  if(nnow<=t[kai])saiki(nx,t[kai],ball+1,nsum,kai+1);
}

int main(){
  while(1){
    cin>>n;
    if(!n)break;
    for(int i=0;i<n;i++)cin >>dis[i]>>t[i];
    sort(t,t+n);
    maxball=0,ans=100000000;
    saiki(0,0,0,0,0);
    if(maxball==n)cout <<"OK "<<ans<<endl;
    else cout<<"NG "<<maxball+1<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1020
memory_kb: 1180
final_rank: 14

code_snippet: // AOJ 1306
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0 ;i < n ; i++)

int p[100],t[100];
int dp[100][55010];
int N;

int main(){
	while( cin >> N && N ){
		rep(i,N) cin >> p[i] >> t[i];
		rep(i,41)rep(j,55010) dp[i][j] = 1e9;
		dp[0][0] = 0;
		rep(i,N){
			rep(k,50010){
				int s = 0;
				int w = 0;
				int pr = 0;
				int maxi = k;
				rep(j,3){
					if( i+j >= N ) break;
					s = (j+1)*abs(pr-p[i+j]); 
					w += abs(pr-p[i+j]);
					if( maxi + s > t[i+j] ) break;
					maxi = t[i+j];
					int next = t[i+j]+(j+2)*p[i+j];
					dp[i+j+1][next] = min(dp[i+j+1][next],dp[i][k]+w+p[i+j]);
					pr = p[i+j];
				}
			}
		}
		int fail = 0;
		rep(i,N+1){
			int ok = 0;
			rep(j,55010)
				if( dp[i][j] != 1e9 ) ok = 1;
			if(!ok){ cout << "NG " << i << endl; fail = 1; break; }
		}
		if( fail ) continue;
		cout << "OK " << *min_element(dp[N],dp[N]+55010) << endl;
	}
}
accuracy: 1
time_ms: 290
memory_kb: 11400
final_rank: 9

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int dp[50][4][8100];
int x[41];
int t[41];
inline int ABS(int a){return max(a,-a);}
int main(){
	int a;
	while(scanf("%d",&a),a){
		int m=0;
		for(int i=0;i<a;i++){
			scanf("%d%d",x+i,t+i);
			m=max(m,t[i]);
		}
		for(int i=0;i<a+1;i++){
			for(int j=0;j<4;j++){
				for(int k=0;k<200*a+10;k++)
					dp[i][j][k]=99999999;
			}
		}
		dp[0][0][0]=0;
		for(int i=0;i<a;i++){
			for(int j=0;j<4;j++){
				for(int k=0;k<200*a+10;k++){
					if(dp[i][j][k]>9999999)continue;
					int t1=(i?x[i-1]:0)*(j+1)+x[i];
					//printf("1: %d %d %d %d\n",i,j,k,dp[i][j][k]+t1);
					if(dp[i][j][k]+t1<=t[i]){
						dp[i+1][1][k+x[i-1]+x[i]]=min(dp[i+1][1][k+x[i-1]+x[i]],t[i]);
					}
					if(j<3){
						int t2=ABS((i?x[i-1]:0)-x[i])*(j+1);
					//	printf("2: %d %d %d %d\n",i,j,k,dp[i][j][k]+t2);
						if(dp[i][j][k]+t2<=t[i]){
							dp[i+1][j+1][k+ABS((i?x[i-1]:0)-x[i])]=min(dp[i+1][j+1][k+ABS((i?x[i-1]:0)-x[i])],t[i]);
						}
					}
				}
			}
		}
		for(int i=a;i>=0;i--){
			int ret=99999999;
			for(int j=0;j<4;j++){
				for(int k=0;k<200*a+10;k++){
					if(dp[i][j][k]<9999999)ret=min(ret,k+x[a-1]);
				}
			}
			if(ret<999999){
				if(i==a)printf("OK %d\n",ret);
				else printf("NG %d\n",i+1);
				break;
			}
		}
	}
}
accuracy: 1
time_ms: 30
memory_kb: 7168
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;

const int MAX_K = 4;
const int MAX_X = 10500;
const int MAX_N = 41;
const int INF = 1e+8;
int n, p[MAX_N], t[MAX_N];
int ans, get;
int dp[MAX_N][MAX_K][MAX_X];

void solve(){
	for(int i=0 ; i < MAX_N ; i++ ){
		for(int j=0 ; j < MAX_K ; j++ ){
			for(int k=0 ; k < MAX_X ; k++ ){
				dp[i][j][k] = INF;
			}
		}
	}
	
	// dp[i][k][x] := sum distance
	dp[0][0][0] = 0;
	for(int i=0 ; i < n ; i++ ){
		for(int k=0 ; k <= 3 ; k++ ){
			for(int x=0 ; x < MAX_X ; x++ ){
				if( dp[i][k][x] != INF ){
					if( k != 0 ){ // go house
						int dist = x;
						double next_time = t[i-1] + (double)dist / (1.0 / (k+1));
						dist += p[i];
						next_time += (double)p[i];
						
						if( next_time <= t[i] ){
							dp[i+1][1][p[i]] = min(dp[i+1][1][p[i]], dp[i][k][x] + dist );
						}
					}
					if( k != 3 ){
						int dist = abs(x - p[i]);
						double prev_time = i? t[i-1] : 0;
						double next_time = prev_time + (double)dist / (1.0 / (k+1));
						
						if( next_time <= t[i] ){
							dp[i+1][k+1][p[i]] = min(dp[i+1][k+1][p[i]], dp[i][k][x] + dist );
						}
					}
				}
			}
		}
	}
	get = 0;
	ans = INF;
	for(int i=0 ; i <= n ; i++ ){
		for(int k=0 ; k <= 3 ; k++ ){
			for(int x=0 ; x < MAX_X ; x++ ){
				if( dp[i][k][x] != INF ){
					get = max(get, i);
					if( i == n ){
						ans = min(ans, dp[i][k][x] + p[i-1]);
					}
				}
			}
		}
	}
}

int main(){
	while( cin >> n , n ){
		for(int i=0 ; i < n ; i++ ){
			cin >> p[i] >> t[i];
		}
		ans = INF;
		solve();
		if( ans != INF ){
			cout << "OK " << ans << endl;
		}else{
			cout << "NG " << get+1 << endl;
		}
	}
}
accuracy: 1
time_ms: 70
memory_kb: 7888
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;

const int MAX_K = 5;
const int MAX_X = 10500;
const int MAX_N = 41;
const int INF = 1e+8;
int n, p[MAX_N], t[MAX_N];
int ans, get;
int dp[MAX_N][MAX_K][MAX_X];

void solve(){
	for(int i=0 ; i < MAX_N ; i++ ){
		for(int j=0 ; j < MAX_K ; j++ ){
			for(int k=0 ; k < MAX_X ; k++ ){
				dp[i][j][k] = INF;
			}
		}
	}
	
	// dp[i][k][x] := sum distance
	dp[0][0][0] = 0;
	for(int i=0 ; i < n ; i++ ){
		for(int k=0 ; k <= 3 ; k++ ){
			for(int x=0 ; x < MAX_X ; x++ ){
				if( dp[i][k][x] != INF ){
					if( k != 0 ){ // go house
						int dist = x;
						double next_time = t[i-1] + (double)dist / (1.0 / (k+1));
						
						dist += p[i];
						next_time += (double)p[i];
						//cout << "time:" << next_time << " t[i]:" << t[i] << endl;
						//cout << "dist:" << dp[i][k][x] + dist << endl;
						if( next_time <= t[i] ){
							//cout << "time:" << next_time << " t[i]:" << t[i] << endl;
							dp[i+1][1][p[i]] = min(dp[i+1][1][p[i]], dp[i][k][x] + dist );
						}
					}
					if( k != 3 ){
						int dist = abs(x - p[i]);
						double prev_time = i? t[i-1] : 0;
						double next_time = prev_time + (double)dist / (1.0 / (k+1));
						//cout << "time:" << next_time << " t[i]:" << t[i] << endl;
						if( next_time <= t[i] ){
							dp[i+1][k+1][p[i]] = min(dp[i+1][k+1][p[i]], dp[i][k][x] + dist );
						}
					}
				}
			}
		}
	}
	get = 0;
	ans = INF;
	for(int i=0 ; i <= n ; i++ ){
		for(int k=0 ; k <= 3 ; k++ ){
			for(int x=0 ; x < MAX_X ; x++ ){
				if( dp[i][k][x] != INF ){
					//cout << "i:" << i << " k:" << k << " x:" << x << " dist:" << dp[i][k][x] << endl;
					get = max(get, i);
					if( i == n ){
						ans = min(ans, dp[i][k][x] + p[i-1]);
					}
				}
			}
		}
	}
}

int main(){
	while( cin >> n , n ){
		for(int i=0; i < n ; i++ ){
			cin >> p[i] >> t[i];
		}
		ans = INF;
		solve();
		if( ans != INF ){
			cout << "OK " << ans << endl;
		}else{
			cout << "NG " << get+1 << endl;
		}
	}
}
accuracy: 1
time_ms: 80
memory_kb: 9568
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
vector<P> info;//position,time

//再帰で全てのパターンをためす
//といっても本当に全てのパターンやったら終わらないので
//現在の距離が答えの距離より大きくなったら最短にはなりえないのでおわらせる


bool iscan;
int ans;
int failedPosition;

void rec(int time,int k,int distance,int position){
  if(distance >= ans)return;

  if(position >= info.size()-1){
    ans = min(ans,distance+info[position].F);
    //cout << "ans = " << ans << endl;
    return;
  }


  int time1,time2;//time1 -> そのまま次へ, time2 -> 一旦家に帰る
  time1 = time+(k+1)*abs(info[position+1].F-info[position].F);
  time2 = time+(k+1)*abs(info[position].F-info[0].F)+info[position+1].F;
  
  if(k == 3){//vechicle can carry at most three balloons at a time. <- importance
    if(time2 <= info[position+1].S){
      rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
      return; // <- dont forget
    }
    else{
      iscan = false;
      failedPosition = position+1;
      return;
    }
   
  }



 
  if(time1 <=info[position+1].S && time2 <=info[position+1].S ){
    rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);
    if(position!=0)rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
  }
  else if(time1<=info[position+1].S){
    rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);
  }
  else if(time2<=info[position+1].S){
    rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
  }
  else {
    iscan = false;
    failedPosition = position+1;
    return;
  } 



}


int main(){
  int n,p,t;
  const int INF=999999999;

  while(cin >> n && n){
    ans = INF;
    info.clear();
    iscan = true;

    info.push_back(P(0,0));//Home 
    for(int i=0;i<n;i++){
      cin >> p >> t;
      info.push_back(P(p,t)); // t_i < t_(i+1)
    }
   
    rec(0,0,0,0);

    if(!iscan && ans == INF){
      cout << "NG " << failedPosition << endl;
    }
    else{
      cout << "OK " << ans << endl;
    }

  }
  return 0;
}
accuracy: 1
time_ms: 540
memory_kb: 1204
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
vector<P> info;//position,time

bool iscan;
int ans;
int failedPosition;

void rec(int time,int k,int distance,int position){
  if(distance >= ans)return;

  if(position >= info.size()-1){
    ans = min(ans,distance+info[position].F);
    //cout << "ans = " << ans << endl;
    return;
  }


  int time1,time2;//time1 -> そのまま次へ, time2 -> 一旦家に帰る
  time1 = time+(k+1)*abs(info[position+1].F-info[position].F);
  time2 = time+(k+1)*abs(info[position].F-info[0].F)+info[position+1].F;
  
  if(k == 3){
    if(time2 <= info[position+1].S){
      rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
      return;
    }
    else{
      iscan = false;
      failedPosition = position+1;
      return;
    }
   
  }



 
  if(time1 <=info[position+1].S && time2 <=info[position+1].S ){
    rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);
    rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
  }
  else if(time1<=info[position+1].S){
    rec(info[position+1].S,k+1,distance+abs(info[position+1].F-info[position].F),position+1);
  }
  else if(time2<=info[position+1].S){
    rec(info[position+1].S,1,distance+abs(info[position].F-info[0].F)+info[position+1].F,position+1);
  }
  else {
    iscan = false;
    failedPosition = position+1;
    return;
  } 



}


int main(){
  int n,p,t;
  const int INF=999999999;

  while(cin >> n && n){
    ans = INF;
    info.clear();
    iscan = true;

    info.push_back(P(0,0));
    for(int i=0;i<n;i++){
      cin >> p >> t;
      info.push_back(P(p,t));
    }
   
    rec(0,0,0,0);

    if(!iscan && ans == INF){
      cout << "NG " << failedPosition << endl;
    }
    else{
      cout << "OK " << ans << endl;
    }

  }
  return 0;
}
accuracy: 1
time_ms: 940
memory_kb: 1204
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;


struct bal{
	int time,loc;
};

int n;
int m_depth;
int res=100000000;
vector<bal> V;

void dfs(int depth,int k,int total){
	m_depth=max(m_depth,depth);
	if(total>=res)
		return;
	if(depth==n){
		total+=V[n-1].loc;
		res=min(res,total);
		return;
	}
	int n_loc=0;
	int room=V[0].time;
	if(depth!=0){
		n_loc=V[depth-1].loc;
		room=V[depth].time-V[depth-1].time;
	}

	int cost=0;
	if(k!=0){
		cost+=n_loc*(k+1);
		cost+=V[depth].loc*1;
		if(cost<=room)
			dfs(depth+1,1,total+n_loc+V[depth].loc);
	}
	cost=0;
	if(k!=3){
		cost+=(abs(V[depth].loc-n_loc))*(k+1);
		if(cost<=room)
			dfs(depth+1,k+1,total+abs(V[depth].loc-n_loc));
	}

}

void solve(){
	res=100000000;
	V.clear();
	m_depth=0;
	for(int i=0;i<n;i++){
		bal now;
		cin>>now.loc>>now.time;
		V.push_back(now);
	}
	m_depth;
	dfs(0,0,0);
	if(m_depth!=n)
		cout<<"NG "<<m_depth+1<<endl;
	else cout<<"OK "<<res<<endl;
}


int main()
{
	while(cin>>n){
		if(n==0) break;
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 1980
memory_kb: 0
final_rank: 15