Problem:
すごろくを作る
太郎君は、子供会の催しでみんなで遊べるようにすごろくを作りました。ゲームをおもしろくするために、「ふりだし」と「あがり」以外のすごろくのマスのいくつかに「６つ進む」、「５つ戻る」のように指示を書き込んでいきました。ルーレットを回して出た数だけ進み、止まったマスに指示が書き込んであれば、その指示に従って移動します。ただし、指示に従って進んだ先のマスの指示には従いません。
ルーレットは１からある数までの間の数を等確率で出すことができるものとします。また、「あがり」に達するより大きな数が出たときや、指示に従うと「あがり」より先に進んでしまうときは「あがり」に移動します。指示に従って戻るときに「ふりだし」より前に戻ってしまうときは「ふりだし」に戻ることにします。
ところが、ルーレットとマスの指示によっては「あがり」にたどりつけない場合が出てきてしまいます。たとえば、図のようなすごろくを作ったとしましょう。１と２しか出ないルーレットを使うと、１，２の順に出れば「あがり」に行けますが、はじめに２が出たらその後は何が出ても永久に「あがり」にはたどり着けません。太郎君は、そうとは知らずに調子に乗ってあちこちのマスに指示を書き込んでしまいました。
そこで、太郎君に代わって、ルーレットとマスの指示によっては、「あがり」にたどり着けない場合が生じるかどうか判定するプログラムを作成してください。
入力
入力は複数のデータセットからなる。入力の終わりはゼロ１つの行で示される。各データセットは以下の形式で与えられる。
max
n
d
1
d
2
.
.
.
d
n
１行目にルーレットが出す数の最大値 max (2 ≤ max ≤ 250) が与えられ、２行目に「ふりだし」と「あがり」以外のマスの数 n (2 ≤ n ≤ 250) が与えられる。続く n 行に各マスの指示を表す数 d
i
(-n ≤ d
i
≤ n) が与えられる。d
i
がゼロのときは指示が書いていないことを、正の数のときは |d
i
| 進む指示を、負の数のときは |d
i
| 戻る指示を表す（ここで、|x| は x の絶対値を表す）。入力される値はすべて整数である。
データセットの数は 100 を超えない。
出力
各データセットごとに判定結果を１行に出力する。ルーレットとマスの指示によっては、「あがり」にたどり着けない場合が生じるときは「NG」、そうでなければ「OK」を出力する。
入力例
3
3
-2
1
0
2
4
2
0
-1
-2
2
2
-2
-2
0
出力例
OK
NG
NG

Submitted solutions:

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

#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rrep(i,n) for(int i=n-1;i>=0;i--)
#define Rrep1(i,n) for(int i=n;i>0;i--)
#define all(a) a.begin(),a.end()
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
#define INF ((ll)4e18)
#define IINF ((int)1e9)
const double PI = 3.141592653589793;//2384626433832795028841971;
template<typename T>inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T>inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }
#define yesno(ans) cout<<((ans)?"Yes\n":"No\n")
bool eq(double a, double b) { return abs(a - b) < 0.0000001; }
//LRUD
const int di[4] = { 0,-1,0,1 };
const int dj[4] = { -1,0,1,0 };
//const int di[8] = { 0,0,1,1,1,-1,-1,-1 };
//const int dj[8] = { 1,-1,0,-1,1,0,-1,1 };

/*ACL
#include <atcoder/all>
using namespace atcoder;
//using mint=modint;
using mint = modint998244353;
//using mint=modint1000000007;
using vm=vector<mint>;
using vvm=vector<vm>;
using vvvm=vector<vvm>;
//*/


int solve(){
    int mx=0;
    while(cin>>mx,mx){
        int N;cin>>N;
        N+=2;
        vi D(N);
        rep1(i,N-2)cin>>D[i];
        D[0]=D[N-1]=0;
        vector<vector<bool>> G(N,vector<bool>(N,0));
        rep(i,N){
            //G[i][i]=1;
            for(int me=1;me<=mx;me++){
                int to=i+me;
                if(to<0)to=0;
                if(to>=N)to=N-1;
                to+=D[to];
                if(to<0)to=0;
                if(to>=N)to=N-1;
                G[i][to]=1;
            }
        }
        
        rep(k,N)rep(i,N)rep(j,N){
            G[i][j]=G[i][j]||(G[i][k]&&G[k][j]);
        }
        // rep(i,N){
        //     rep(j,N)cout<<G[i][j];cout<<endl;
        // }
        bool isng=0;
        rep(i,N){
            if(G[0][i]==1&&G[i][N-1]==0){
                //cout<<i<<" "<<endl;
                isng=1;
            }
        }
        if(!isng){
            cout<<"OK\n";
        }else{
            cout<<"NG\n";
        }

    }
    return 0;
}

int main()
{
    //std::cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    //cin>>T;
    while (T--)
    {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 2150
memory_kb: 3348
final_rank: 14

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cctype>
#include<sstream>
#include<regex>
#include<bitset>
#include<random>
#include<complex>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int Mod=998244353;
const int inf=mod*mod;
const double eps=1e-9;
vector<int>G[252],revG[252];
bool used1[252],used2[252];
void dfs1(int v){
	used1[v]=1;
	for(auto x:G[v])
		if(!used1[x])
			dfs1(x);
}
void dfs2(int v){
	used2[v]=1;
	for(auto x:revG[v])
		if(!used2[x])
			dfs2(x);
}
signed main(){
	int mx;
	while(cin>>mx,mx){
		int n,d;
		map<int,int>mp;
		cin>>n;
		for(int i=0;i<=n+1;i++){
			G[i].clear();
			revG[i].clear();
		}
		fill(used1,used1+n+2,0);
		fill(used2,used2+n+2,0);
		for(int i=1;i<=n;i++){
			cin>>d;
			mp[i]=d;
		}
		for(int i=0;i<=n;i++){
			for(int j=1;j<=mx;j++){
				G[i].push_back(clamp(i+j+mp[i+j],0ll,n+1));
				revG[clamp(i+j+mp[i+j],0ll,n+1)].push_back(i);
			}
		}
		dfs1(0);
		dfs2(n+1);
		bool ans=0;
		for(int i=0;i<=n+1;i++)
			if(used1[i]&&!used2[i])
				ans=1;
		cout<<(ans?"NG":"OK")<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3492
final_rank: 5

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

int main(){
    while(true){
        int rmax;
        cin >> rmax;
        if(rmax == 0) break;
        int n;
        cin >> n;
        vector<int> d(n+1);
        for(int i = 1; i <= n; i++) cin >> d[i];

        vector<vector<int>> G(n+2);
        for(int i = 0; i <= n; i++){
            for(int j = i+1; j <= min(i+rmax, n+1); j++){
                int nex = j + d[j];
                if(nex < 0) nex = 0;
                if(nex > n+1) nex = n+1;
                G[i].push_back(nex);
            }
        }
        
        vector<vector<int>> isReachable(n+2, vector<int>(n+2, 0));
        for(int start = 0; start < n+2; start++){
            isReachable[start][start] = 1;
            queue<int> Q;
            Q.push(start);
            while(Q.size()){
                int cur = Q.front(); Q.pop();
                for(auto &p: G[cur]){
                    if(isReachable[start][p] == 0){
                        isReachable[start][p] = 1;
                        Q.push(p);
                    }
                }
            }
        }

        string ans = "OK";
        if(isReachable[0][n+1] == 0) ans = "NG";
        for(int i = 1; i <= n; i++){
            if(isReachable[0][i] == 1 && isReachable[i][n+1] == 0){
                ans = "NG";
            }
        }

        cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3408
final_rank: 4

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin Header {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// End Header }}}

// Edge {{{
template <class Weight>
struct Edge {
    size_t from, to;
    Weight weight;

    Edge() {}
    Edge(size_t from, size_t to, Weight weight = 1) :
        from(from), to(to), weight(weight)
    {}

    bool operator<(const Edge &rhs) const {
        return weight < rhs.weight;
    }

    bool operator>(const Edge &rhs) const {
        return weight > rhs.weight;
    }

    operator size_t() const {
        return to;
    }
};
// }}}

// Graph {{{
template <class Weight>
class Graph : public vector<vector<Edge<Weight>>> {
    using graph = vector<vector<Edge<Weight>>>;

public:
    Graph() {}
    Graph(const size_t V) : graph(V) {}

    void connect(size_t from, size_t to, Weight weight = 1) {
        (*this)[from].emplace_back(from, to, weight);
    }

    friend ostream& operator<<(ostream &strm, const Graph &G) {
        for (size_t v = 0; v < G.size(); ++v) {
            strm << '[' << setw(2) << v << ']';
            for (const auto &e: G[v]) {
                strm << ' ' << setw(2) << e.to;
            }
            strm << '\n';
        }
        return strm;
    }
};
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(20);

    intmax_t M;
    while (cin >> M, M) {
        var(intmax_t, N);
        vector<intmax_t> d(N + 2);
        reps(i, 1, N) input(d[i]);

        const auto clamp = [&](intmax_t v) -> intmax_t {
            if (v < 0) {
                return 0;
            } else if (N + 1 < v) {
                return N + 1;
            } else {
                return v;
            }
        };

        vector<bool> seen(N + 2, false);
        const auto dfs = makeFixPoint([&](auto dfs, size_t cur) -> bool {
            seen[cur] = true;
            if (cur == N + 1) return true;
            bool ret = false;
            reps(k, 1, M) {
                const intmax_t v = clamp(cur + k);
                const intmax_t nxt = clamp(v + d[v]);
                if (!seen[nxt]) ret |= dfs(nxt);
            }
            return ret;
        });

        dfs(0);

        auto can_reach = seen;

        bool valid = true;
        reps(i, 0, N) {
            seen.assign(N + 2, false);
            if (can_reach[i] && !dfs(i)) {
                valid = false;
                break;
            }
        }

        output((valid) ? "OK" : "NG");
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3208
final_rank: 3

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin Header {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// End Header }}}

// Edge {{{
template <class Weight>
struct Edge {
    size_t from, to;
    Weight weight;

    Edge() {}
    Edge(size_t from, size_t to, Weight weight = 1) :
        from(from), to(to), weight(weight)
    {}

    bool operator<(const Edge &rhs) const {
        return weight < rhs.weight;
    }

    bool operator>(const Edge &rhs) const {
        return weight > rhs.weight;
    }

    operator size_t() const {
        return to;
    }
};
// }}}

// Graph {{{
template <class Weight>
class Graph : public vector<vector<Edge<Weight>>> {
    using graph = vector<vector<Edge<Weight>>>;

public:
    Graph() {}
    Graph(const size_t V) : graph(V) {}

    void connect(size_t from, size_t to, Weight weight = 1) {
        (*this)[from].emplace_back(from, to, weight);
    }

    friend ostream& operator<<(ostream &strm, const Graph &G) {
        for (size_t v = 0; v < G.size(); ++v) {
            strm << '[' << setw(2) << v << ']';
            for (const auto &e: G[v]) {
                strm << ' ' << setw(2) << e.to;
            }
            strm << '\n';
        }
        return strm;
    }
};
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(20);

    intmax_t M;
    while (cin >> M, M) {
        var(intmax_t, N);
        vector<intmax_t> d(N + 2);
        reps(i, 1, N) input(d[i]);

        const auto clamp = [&](intmax_t v) -> intmax_t {
            if (v < 0) {
                return 0;
            } else if (N + 1 < v) {
                return N + 1;
            } else {
                return v;
            }
        };

        auto to = makeVector<bool>(false, N + 2, N + 2);
        reps(i, 0, N + 1) to[i][i] = true;

        reps(i, 0, N) reps(j, 1, M) {
            const intmax_t v = clamp(i + j);
            const intmax_t w = clamp(v + d[v]);
            to[i][w] = true;
        }

        vector<bool> seen(N + 2, false);
        vector<int> dp(N + 2, -1);
        const auto dfs = makeFixPoint([&](auto dfs, size_t cur) -> bool {
            if (cur == N + 1) return true;
            seen[cur] = true;
            auto& ret = dp[cur];
            if (~ret) return ret;
            ret = false;
            reps(nxt, 0, N + 1) {
                if (!seen[nxt] && to[cur][nxt]) ret |= dfs(nxt);
            }
            return ret;
        });

        dfs(0);

        bool valid = true;
        reps(i, 0, N) {
            if (seen[i] && !dp[i]) {
                valid = false;
                break;
            }
        }

        output((valid) ? "OK" : "NG");
    }

    return 0;
}
accuracy: 0.5
time_ms: 20
memory_kb: 3160
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin Header {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// End Header }}}

// Edge {{{
template <class Weight>
struct Edge {
    size_t from, to;
    Weight weight;

    Edge() {}
    Edge(size_t from, size_t to, Weight weight = 1) :
        from(from), to(to), weight(weight)
    {}

    bool operator<(const Edge &rhs) const {
        return weight < rhs.weight;
    }

    bool operator>(const Edge &rhs) const {
        return weight > rhs.weight;
    }

    operator size_t() const {
        return to;
    }
};
// }}}

// Graph {{{
template <class Weight>
class Graph : public vector<vector<Edge<Weight>>> {
    using graph = vector<vector<Edge<Weight>>>;

public:
    Graph() {}
    Graph(const size_t V) : graph(V) {}

    void connect(size_t from, size_t to, Weight weight = 1) {
        (*this)[from].emplace_back(from, to, weight);
    }

    friend ostream& operator<<(ostream &strm, const Graph &G) {
        for (size_t v = 0; v < G.size(); ++v) {
            strm << '[' << setw(2) << v << ']';
            for (const auto &e: G[v]) {
                strm << ' ' << setw(2) << e.to;
            }
            strm << '\n';
        }
        return strm;
    }
};
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(20);

    intmax_t M;
    while (cin >> M, M) {
        var(intmax_t, N);
        vector<intmax_t> d(N + 2);
        reps(i, 1, N) input(d[i]);

        const auto clamp = [&](intmax_t v) -> intmax_t {
            if (v < 0) {
                return 0;
            } else if (N + 1 < v) {
                return N + 1;
            } else {
                return v;
            }
        };

        auto to = makeVector<bool>(false, N + 2, N + 2);
        reps(i, 0, N + 1) to[i][i] = true;

        reps(i, 0, N) reps(j, 1, M) {
            const intmax_t v = clamp(i + j);
            const intmax_t w = clamp(v + d[v]);
            to[i][w] = true;
        }

        vector<bool> seen(N + 2, false);
        vector<int> dp(N + 2, -1);
        const auto dfs = makeFixPoint([&](auto dfs, size_t cur) -> bool {
            if (cur == N + 1) return true;
            seen[cur] = true;
            to[0][cur] = true;
            auto& ret = dp[cur];
            if (~ret) return ret;
            ret = false;
            reps(nxt, 0, N + 1) {
                if (!seen[nxt] && to[cur][nxt]) ret |= dfs(nxt);
            }
            return ret;
        });

        dfs(0);

        bool valid = true;
        reps(i, 0, N) {
            if (to[0][i] && !dp[i]) {
                valid = false;
                break;
            }
        }

        output((valid) ? "OK" : "NG");
    }

    return 0;
}
accuracy: 0.5
time_ms: 20
memory_kb: 3248
final_rank: 19

code_snippet: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;
int N;
int G[300][300];

void warshall() {
	REP(i, N + 2) {
		REP(j, N + 2) {
			REP(k, N + 2) {
				G[j][k] = min(G[j][k], G[j][i] + G[i][k]);
			}
		}
	}
}


string solve(int M) {
	cin >> N;

	VI D(N);
	REP(i, N)cin >> D[i];
	queue<int>Q;
	vector<bool>F(N + 1, false);
	F[0] = 1;
	Q.push(0);
	while (!Q.empty()) {
		int from = Q.front(); Q.pop();
		eFOR(i, 1, M) {
			int to = from + i;
			if (to > N + 1)break;
			if (to == N + 1) {
				//G[from].push_back(to);
				G[from][to] = 1;
			}
			else {
				to += D[to - 1];
				if (to < 0)to = 0;
				if (to > N + 1)to = N + 1;
				//if (from != to) {
					//G[from].push_back(to);
					//cout << from << " " << to << endl;
					G[from][to] = 1;
					if (!F[to]) {
						F[to] = 1;
						Q.push(to);
					}
				//}
			}
		}
	}
	warshall();
	eREP(i, N) {
		//cout << i + 1 << " " << G[0][i + 1] << " " << G[i + 1][N + 1] << endl;
		if (G[0][i] != INF && G[i][N + 1] == INF) {
			//cout << i + 1 << endl;
			return "NG";
		}
	}
	return "OK";
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int M = 1;
	while (M) {
		REP(i, 300)REP(j, 300)G[i][j] = INF;
		cin >> M;
		if (!M)return 0;
		cout << solve(M) << '\n';
	}

	return 0;
}
accuracy: 1
time_ms: 1330
memory_kb: 3896
final_rank: 13

code_snippet: #include<bits/stdc++.h>

using namespace std;

int Step, N;
int NodeInfo[252];
bool Result[252][252];


bool solve() {

	for (int k = 0; k <= N + 1; ++k) {
		for (int i = 0; i <= N + 1; ++i) {
			for (int j = 0; j <= N + 1; ++j) {
				if (Result[i][k] && Result[k][j]) {
					Result[i][j] = true;
				}
			}
		}
	}

	for (int i = 0; i <= N; ++i) {
		if (Result[0][i] && (!Result[i][N + 1])) {
			return false;
		}
	}

	return true;
}

void createMapData() {
	int currentNode;

	for (int i = 0; i <= N; ++i) {
		for (int j = 1; j <= Step; ++j) {
			currentNode = i + j;
			if (currentNode > N) {
				Result[i][N + 1] = true;
				break;
			}

			currentNode = max(0, currentNode += NodeInfo[currentNode]);
			if (currentNode >= N) {
				Result[i][N + 1] = true;
			}
			else {
				Result[i][currentNode] = true;
			}
		}
	}
}

int main() {

	while (cin >> Step, Step) {
		memset(NodeInfo, 0, sizeof(NodeInfo));
		memset(Result, false, sizeof(Result));
		cin >> N;
		for (int i = 1; i <= N; ++i) {
			cin >> NodeInfo[i];
		}

		createMapData();
		if (solve()) {
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 3144
final_rank: 6

code_snippet: #include<bits/stdc++.h>

using namespace std;

int Step, N;
int NodeInfo[252];
bool Result[252][252], Used[252];


void solve() {

	for (int k = 0; k <= N+1; ++k) {
		for (int i = 0; i <= N+1; ++i) {
			for (int j = 0; j <= N+1; ++j) {
				if (i == j || Result[i][j]) {
					continue;
				}
				if (Result[i][k] && Result[k][j]) {
					Result[i][j] = true;
				}
			}
		}
	}

	for (int i = 0; i <= N; ++i) {
		if (Result[0][i] && (!Result[i][N + 1])) {
			cout << "NG" << endl;
			return;
		}
	}
	
	cout << "OK" << endl;
}

void createMapDataByBFS() {
	int startNode, currentNode;
	queue<int> nodeList;

	nodeList.push(0);
	while (!nodeList.empty()) {
		startNode = nodeList.front();
		nodeList.pop();
		if (Used[startNode]) {
			continue;
		}
		Used[startNode] = true;

		for (int i = 1; i <= Step; ++i) {
			currentNode = startNode + i;
			if (currentNode > N) {
				Result[startNode][N+1] = true;
				break;
			}

			currentNode = max(0, currentNode += NodeInfo[currentNode]);
			if (currentNode >= N) {
				Result[startNode][N+1] = true;
			}
			else {
				Result[startNode][currentNode] = true;
				nodeList.push(currentNode);
			}
		}
	}
}

void createMapData() {
	int currentNode;

	for (int i = 0; i <= N; ++i) {
		for (int j = 1; j <= Step; ++j) {
			currentNode = i + j;
			if (currentNode > N) {
				Result[i][N + 1] = true;
				break;
			}

			currentNode = max(0, currentNode += NodeInfo[currentNode]);
			if (currentNode > N) {
				Result[i][N + 1] = true;
			}
			else {
				Result[i][currentNode] = true;
			}
		}
	}
}

int main() {

	while (cin >> Step, Step) {
		memset(NodeInfo, 0, sizeof(NodeInfo));
		memset(Result, false, sizeof(Result));
		memset(Used, false, sizeof(Used));
		cin >> N;
		for (int i = 1; i <= N; ++i) {
			cin >> NodeInfo[i];
		}

		//createMapDataByBFS();
		createMapData();
		solve();
	}

	return 0;
}
accuracy: 1
time_ms: 1840
memory_kb: 3172
final_rank: 12

code_snippet: #include<bits/stdc++.h>

using namespace std;

int Step, N;
int NodeInfo[252];
bool Result[252][252], Used[252];


void solve() {

	for (int k = 0; k <= N+1; ++k) {
		for (int i = 0; i <= N+1; ++i) {
			for (int j = 0; j <= N+1; ++j) {
				if (i == j || Result[i][j]) {
					continue;
				}
				if (Result[i][k] && Result[k][j]) {
					Result[i][j] = true;
				}
			}
		}
	}

	for (int i = 0; i <= N; ++i) {
		if (Result[0][i] && (!Result[i][N + 1])) {
			cout << "NG" << endl;
			return;
		}
	}
	
	cout << "OK" << endl;
}

void createMapData() {
	int startNode, currentNode;
	queue<int> nodeList;

	nodeList.push(0);
	while (!nodeList.empty()) {
		startNode = nodeList.front();
		nodeList.pop();
		if (Used[startNode]) {
			continue;
		}
		Used[startNode] = true;

		for (int i = 1; i <= Step; ++i) {
			currentNode = startNode + i;
			if (currentNode > N) {
				Result[startNode][N+1] = true;
				break;
			}

			currentNode = max(0, currentNode += NodeInfo[currentNode]);
			if (currentNode >= N) {
				Result[startNode][N+1] = true;
			}
			else {
				Result[startNode][currentNode] = true;
				nodeList.push(currentNode);
			}
		}
	}
}

int main() {

	while (cin >> Step, Step) {
		memset(NodeInfo, 0, sizeof(NodeInfo));
		memset(Result, false, sizeof(Result));
		memset(Used, false, sizeof(Used));
		cin >> N;
		for (int i = 1; i <= N; ++i) {
			cin >> NodeInfo[i];
		}

		createMapData();
		solve();
	}

	return 0;
}
accuracy: 1
time_ms: 1680
memory_kb: 3212
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int M;
    while(cin >> M, M){
        int n;
        cin >> n;
        vector<int> v(n);
        for (auto &&i : v) scanf("%d", &i);
        vector<int> to(n+2);
        to.back() = n+1;
        for (int i = 0; i < n; ++i) {
            if(v[i] < 0) to[i+1] = max(0, i+1+v[i]);
            else to[i+1] = min(n+1, i+1+v[i]);
        }
        vector<vector<int>> dp(n+2, vector<int>(n+2, INF<int>));
        for (int i = 0; i <= n; ++i) {
            for (int j = 1; j <= M; ++j) {
                int t = to[min(n+1, i+j)];
                dp[i][t] = 1;
            }
        }
        for (int k = 0; k <= n+1; ++k) {
            for (int i = 0; i <= n+1; ++i) {
                for (int j = 0; j <= n+1; ++j) {
                    dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
                }
            }
        }
        int ok = 1;
        for (int i = 0; i <= n; ++i) {
            if(dp[0][i] != INF<int> && dp[i][n+1] == INF<int>){
                ok = 0;
            }
        }
        puts(ok ? "OK" : "NG");
    }
    return 0;
}
accuracy: 1
time_ms: 1380
memory_kb: 3248
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T> inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

int main() {
    init();

    while (1) {
        int dice;
        cin >> dice;
        if (dice == 0)break;

        int n;
        cin >> n;
        n += 2;
        VI d(n, 0);
        FOR(i, 1, n - 1)cin >> d[i];

        vector<vector<bool>> g(n, vector<bool>(n, false));
        FOR(i, 0, n)g[i][i] = true;
        FOR(i, 0, n) {
            eFOR(j, 1, dice) {
                int next = i + j + d[min(i + j, n - 1) ];
                chmax(next, 0), chmin(next, n - 1);
                g[i][next] = true;
            }
        }
        FOR(k, 0, n)FOR(i, 0, n)FOR(j, 0, n) {
            if (g[i][k] && g[k][j])g[i][j] = true;
        }
        string ans = "OK\n";
        FOR(i, 0, n)if (g[0][i]) {
            if (!g[i][n - 1])ans = "NG\n";
        }
        cout << ans;
    }
    
}
accuracy: 1
time_ms: 1050
memory_kb: 3176
final_rank: 9

code_snippet: #include <iostream>
using namespace std;
#include <vector>
int main(){
  int m,n;
  while(true){
    cin >> m;
    if(m == 0)break;
    cin >> n;
    vector<int> d(n*2);
    vector<vector<int> >p(n*3,vector<int>(n*3,0));
    for(int i = 0; i < 2*n; i++)d[i] = 0;
    for(int i = 1; i <= n; i++)cin >> d[i];
    
    for(int i = 0; i <= n; i++){
      for(int j = 1; j <= m; j++){
	int pp= i+j+d[i+j];
	if(pp < 0)pp = 0;
	if(pp > n+1)pp = n+1;
	p[i][pp] = 1;
      }
    }
    
 
    for(int j = 0; j <= n+1; j++){
      for(int i = 0; i <= n+1; i++){
	for(int k = 0; k <= n+1; k++){
	  if(p[j][i]&&p[i][k]) p[j][k] = 1;
	}
      }
    }

      
    int ans = true;
    for(int i = 0;i < n+1; i++){
      if(p[0][i]==1&&p[i][n+1] == 0)ans = false;
    }
    if(ans)cout << "OK" << endl;
    else cout << "NG" << endl;
  }
}
accuracy: 1
time_ms: 1120
memory_kb: 4896
final_rank: 16

code_snippet: #include <iostream>
using namespace std;
#include <vector>
int main(){
  int m,n;
  while(true){
    cin >> m;
    if(m == 0)break;
    cin >> n;
    vector<int> d(n*2);
    vector<vector<int> >p(n*3,vector<int>(n*3,0));
    for(int i = 0; i < 2*n; i++)d[i] = 0;
    for(int i = 1; i <= n; i++)cin >> d[i];
    
    for(int i = 0; i <= n; i++){
      for(int j = 1; j <= m; j++){
	int pp= i+j+d[i+j];
	if(pp < 0)pp = 0;
	if(pp > n+1)pp = n+1;
	p[i][pp] = 1;
      }
    }
    
 
    for(int j = 0; j <= n+1; j++){
      for(int i = 0; i <= n+1; i++){
	for(int k = 0; k <= n+1; k++){
	  if(p[j][k]&&p[k][i]) p[j][i] = 1;
	}
      }
    }

      
    int ans = true;
    for(int i = 0;i < n+1; i++){
      if(p[0][i]==1&&p[i][n+1] == 0)ans = false;
    }
    if(ans)cout << "OK" << endl;
    else cout << "NG" << endl;
  }
}
accuracy: 1
time_ms: 1160
memory_kb: 4924
final_rank: 17

code_snippet: #include <iostream>
using namespace std;
#include <vector>
int main(){
  int m,n;
  while(true){
    cin >> m;
    if(m == 0)break;
    cin >> n;
    vector<int> d(n*2);
    vector<vector<int> >p(n*3,vector<int>(n*3,0));
    for(int i = 0; i < 2*n; i++)d[i] = 0;
    for(int i = 1; i <= n; i++)cin >> d[i];
    
    for(int i = 0; i <= n; i++){
      for(int j = 1; j <= m; j++){
	int pp= i+j+d[i+j];
	if(pp < 0)pp = 0;
	if(pp > n+1)pp = n+1;
	p[i][pp] = 1;
      }
    }
    for(int j = 0; j <= n+1; j++){
      for(int i = 0; i <= n+1; i++){
	for(int k = 0; k <= n+1; k++){
	  if(p[i][j]&&p[j][k]) p[i][k] = 1;
	}
      }
    }
   
      
    int ans = true;
    for(int i = 0;i < n+1; i++){
      if(p[0][i]==1&&p[i][n+1] == 0)ans = false;
    }
    if(ans)cout << "OK" << endl;
    else cout << "NG" << endl;
  }
}
accuracy: 1
time_ms: 940
memory_kb: 4972
final_rank: 15

code_snippet: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;



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

	while (1) {
		
		int ma, N;
		cin >> ma;
		if (ma == 0) {
			break;
		}
		cin >> N;
		VI A(N);
		REP(i, N) {
			cin >> A[i];
		}
		int G[300][300] = { 0 };
		REP(i, N + 1) {
			eFOR(j,1, ma) {
				int a = i + j + A[min(i + j - 1, N - 1)];
				if (a <= 0)a = 0;
				else if (a >= N + 1)a = N + 1;
				G[i][a] = 1;
			}
		}
		/*REP(i, N + 1) {
			REP(j, N + 2) {
				cout << G[i][j];
			}
			cout << endl;
		}*/
		for (int k = 0; k < N + 2; k++) {
			for (int i = 0; i < N + 2; i++) {
				for (int j = 0; j < N + 2; j++) {
					if (G[i][k] && G[k][j]) G[i][j] = 1;
				}
			}
		}
		bool valid = true;
		for (int i = 0; i <= N; i++) {
			if (G[0][i] && !G[i][N + 1]) valid = false;
		}
		cout << (valid ? "OK" : "NG") << endl;

	}
	return 0;
}
accuracy: 0.5
time_ms: 900
memory_kb: 3916
final_rank: 20

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

using int64 = long long;

template<typename T>
using p_que = priority_queue<T>;
template<typename T>
using rp_que = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = (1 << 30) - 1;
constexpr int64 INF64 = (1ll << 60) - 1;

#define rep(i, N) for(int i=0;i<(int)(N);++i)
#define fs first
#define sc second
#define e_b emplace_back
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	return os << "P(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "[";
	for (auto& e : v) os << e << ", ";
	return os << "]";
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& m) {
	os << "{" << endl;
	for (auto& e : m) os << "(" << e.first << ", " << e.second << ")" << endl;
	return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& s) {
	os << "{" << endl;
	for (auto& e : s) os << ", " << e << endl;
	return os << "}";
}

template<typename T>
vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int64 gcd(int64 x, int64 y) {
	if (x == 0 || y == 0) return 0;
	int64 r;
	while ((r = y % x) != 0) {
		y = x;
		x = r;
	}
	return x;
}
int64 lcm(int64 x, int64 y) {
	if (x == 0 || y == 0) return 0;
	return x / gcd(x, y) * y;
}

int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };

void Main();
signed main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(30);
	Main();
}

/*----------------------------Insert from here!----------------------------*/

template <int mod>
struct ModInt {
	int x;

	ModInt() : x(0) {}

	ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

	ModInt& operator+=(const ModInt& p) {
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt& operator-=(const ModInt& p) {
		if ((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt& operator*=(const ModInt& p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}

	ModInt& operator/=(const ModInt& p) {
		*this *= p.inverse();
		return *this;
	}

	ModInt operator-() const { return ModInt(-x); }

	ModInt operator+(const ModInt& p) const { return ModInt(*this) += p; }

	ModInt operator-(const ModInt& p) const { return ModInt(*this) -= p; }

	ModInt operator*(const ModInt& p) const { return ModInt(*this) *= p; }

	ModInt operator/(const ModInt& p) const { return ModInt(*this) /= p; }

	bool operator==(const ModInt& p) const { return x == p.x; }

	bool operator!=(const ModInt& p) const { return x != p.x; }

	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}

	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while (n > 0) {
			if (n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

	friend ostream& operator<<(ostream& os, const ModInt& p) {
		return os << p.x;
	}

	friend istream& operator>>(istream& is, ModInt& a) {
		int64_t t;
		is >> t;
		a = ModInt< mod >(t);
		return (is);
	}

	static int get_mod() { return mod; }
};

template <typename T>
struct Edge {
	int from, to;
	T cost;

	Edge() : from(0), to(0), cost(0) {}
	Edge(int to, T cost) : from(-1), to(to), cost(cost) {}
	Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
	Edge& operator=(const int& x) {
		to = x;
		return *this;
	}
};

template <typename T>
using Edges = vector<Edge<T>>;
template <typename T>
using wGraph = vector<Edges<T>>;

/* Function from here! */

template <typename T>
vector<T> dijkstra(wGraph<T>& g, int s) {
	const auto INF = numeric_limits<T>::max();
	vector<T> dist(g.size(), INF);

	using P = pair<T, int>;
	priority_queue<P, vector<P>, greater<P>> que;
	dist[s] = 0;
	que.emplace(dist[s], s);
	while (!que.empty()) {
		T cost;
		int idx;
		tie(cost, idx) = que.top();
		que.pop();
		if (dist[idx] < cost) continue;
		for (auto& e : g[idx]) {
			T next_cost = cost + e.cost;
			if (dist[e.to] <= next_cost) continue;
			dist[e.to] = next_cost;
			que.emplace(dist[e.to], e.to);
		}
	}

	return dist;
}

/*----------------------------Insert above here----------------------------*/

void Main() {
	int max, n;
	while (true) {
		cin >> max;
		if (max == 0) break;
		cin >> n;

		vector<int> d(n + 2);
		for (int i = 1; i <= n; ++i) cin >> d[i];

		wGraph<int> G(n + 2);
		vector<int> used(n + 2);
		stack<int> stk;
		stk.emplace(0);
		while (!stk.empty()) {
			int idx = stk.top(); stk.pop();
			if (used[idx]) continue;
			used[idx] = 1;
			for (int i = 1; i <= max; ++i) {
				int next = idx + i;
				if (next >= n + 2) break;
				next += d[next];
				if (next >= n + 2) next = n + 1;
				else if (next < 0) next = 0;
				G[idx].emplace_back(next, 1);

				if (used[next]) continue;
				stk.emplace(next);
			}
		}

		/*
		for(int i=0; i<n+1; ++i) {
			cout << i << " ";
			for (auto& k : G[i]) cout << k.to << " ";
			cout << endl;
		}
		*/

		for (int i = 0; i < n + 1; ++i) {
			if (!used[i]) continue;
			auto d = dijkstra(G, i);
			if (d[n + 1] == numeric_limits<int>::max()) {
				cout << "NG" << endl;
				goto LABEL;
			}
		}
		cout << "OK" << endl;
	LABEL:
		;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3216
final_rank: 2

code_snippet: #include "bits/stdc++.h"
// Custom Header {{{
#define ALL(x) x.begin(), x.end()
#define endl '\n'
using namespace std;
using i64 = long long;
using pii = pair<i64, i64>;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}}

int K, N;
int d[300];
int START = 0, GOAL;
bool reach[300];
bool visited[300];
vector<vector<int>> G;

void addEdge(int s)
{
    for (int j = 1; j <= K; ++j) {
        int t = min(GOAL, s + j);
        G[s].push_back(max(0, min(GOAL, t + d[t])));
    }
}

void dfs(int u)
{
    if (!visited[u]) {
        visited[u] = true;
        for (int v : G[u]) dfs(v);
    }
}

bool canGoal(int u)
{
    if (u == GOAL) return true;

    if (visited[u]) return false;
    visited[u] = true;

    bool ret = false;
    for (int v : G[u]) {
        ret |= canGoal(v);
    }
    return ret;
}

signed main()
{
    cin.tie(nullptr); ios::sync_with_stdio(false);


    while(cin >> K, K) {
        cin >> N;
        memset(d, 0, sizeof(d));
        for (int i = 1; i <= N; ++i) cin >> d[i];
        GOAL = N+1;

        G = vector<vector<int>>(N+2);
        for (int i = 0; i <= N; ++i) {
            addEdge(i);
        }

        /*
        cout << "-------------------------------------" << endl;
        cout << ++__  << ' ' << N << ' ' << K << endl;
        for (int i = 1; i <= N; ++i) cout << d[i] << ' ';
        cout << endl;

        for (int i = 0; i <= GOAL; ++i) {
            printf("[%d] ", i);
            for (int u : G[i]) {
                printf(" %d", u);
            }
            printf("\n");
        }
        */

        memset(visited, false, sizeof(visited));
        dfs(0);

        int reachCount = 0;
        int canGoalCount = 0;

        memcpy(reach, visited, sizeof(reach));
        for (int i = 1; i <= GOAL; ++i) {
            if (reach[i]) {
                //printf("reach : %d\n", i);
                ++reachCount;
                memset(visited, false, sizeof(visited));
                if (canGoal(i)) {
                    ++canGoalCount;
                }
            }
        }

        cout << (canGoalCount > 0 && reachCount == canGoalCount ? "OK" : "NG") << endl;

    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3208
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

int a[1000], used[1000], b[300][300], used2[1000];
vector<int> G[300];
void dfs(int u){
	if(used[u])return;
	used[u] = 1;
	rep(i,G[u].size()){
		dfs(G[u][i]);
	}
}

void dfs2(int u, int R, int t){
	if(used2[u])return;
	used2[u] = 1;
	int i = u;
	rrep(j,R){
		if(i+j >= t){b[i][t] = 1;dfs2(t, R, t);continue;}
		if(i+j+a[i+j] < 0){b[i][0] = 1;dfs2(0, R, t);continue;}
		if(i+j+a[i+j] >= t){b[i][t] = 1;dfs2(t, R, t);continue;}
		b[i][i+j+a[i+j]] = 1;dfs2(i+j+a[i+j], R, t);
	}
}

void init(){
	rep(i,300)rep(j,300)b[i][j] = 0;
	fill(used2, used2+1000, 0);
	fill(used, used+1000, 0);
	rep(i,300)G[i].clear();
}

signed main(){
	while(1){
		int R, n;
		scanf("%lld", &R);
		if(R == 0)break;
		scanf("%lld", &n);
		rrep(i,n)scanf("%lld", &a[i]);
		int s = 0, t = n+1;
		init();
		dfs2(0, R, t);
		rep(i,t+1)rep(j,t+1){if(b[j][i])G[i].pb(j);}
		dfs(t);
		
		int ans = 1;
		rep(i,t+1)if(used[i] != used2[i])ans = 0;
		printf("%s\n", ans ? "OK" : "NG");
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3944
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

int a[1000], used[1000], b[300][300], used2[1000];
vector<int> G[300];
void dfs(int u){
	if(used[u])return;
	used[u] = 1;
	rep(i,G[u].size()){
		dfs(G[u][i]);
	}
}

void dfs2(int u, int R, int t){
	if(used2[u])return;
	used2[u] = 1;
	int i = u;
	rrep(j,R){
		if(i+j >= t){b[i][t] = 1;dfs2(t, R, t);continue;}
		if(i+j+a[i+j] < 0){b[i][0] = 1;dfs2(0, R, t);continue;}
		if(i+j+a[i+j] >= t){b[i][t] = 1;dfs2(t, R, t);continue;}
		b[i][i+j+a[i+j]] = 1;dfs2(i+j+a[i+j], R, t);
	}
}

void init(){
	rep(i,300)rep(j,300)b[i][j] = 0;
	fill(used2, used2+1000, 0);
	fill(used, used+1000, 0);
	rep(i,300)G[i].clear();
}

signed main(){
	while(1){
		int R, n;
		scanf("%lld", &R);
		if(R == 0)break;
		scanf("%lld", &n);
		rrep(i,n)scanf("%lld", &a[i]);
		int s = 0, t = n+1;
		init();
		dfs2(0, R, t);
		rep(i,t+1)rep(j,t+1){if(b[j][i])G[i].pb(j);}
		dfs(t);
		
		int ans = 1;
		rep(i,t+1)if(used[i] != used2[i])ans = 0;
		/*rep(i,t+1)printf("%lld ", used2[i]);printf("\n");
		rep(i,t+1)printf("%lld ", used[i]);printf("\n");
		rep(i,t+1)printf("%lld ", (int)(G[i].size()));printf("\n");*/
		/*rep(i,t+1){
			rep(j,t+1)printf("%lld ", b[i][j]);
			printf("\n");
		}*/
		printf("%s\n", ans ? "OK" : "NG");
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3980
final_rank: 8