alexjercan/bugnet huggingface dataset:
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n && n) {
    long long int p;
    long long int jo[65005];
    long long int total = 0;
    for (int i = 0; i < n; i++) {
      cin >> p;
      total += p;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> jo[i];
    }
    sort(jo, jo + (n - 1), greater<long long int>());
    long long int ans = 0;
    long long int k = n;
    long long tmp = total;
    for (int i = 0; k > 0; k--, i++) {
      ans = max(ans, tmp * k);
      tmp += jo[i];
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n && n) {
    int p;
    int jo[65005];
    int total = 0;
    for (int i = 0; i < n; i++) {
      cin >> p;
      total += p;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> jo[i];
    }
    sort(jo, jo + (n - 1), greater<int>());
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int k = n - i;
      int tmp = total;
      for (int j = 0; j < i; j++) {
        tmp += jo[j];
      }
      ans = max(ans, tmp * k);
    }
    cout << ans << endl;
  }
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

ll ans(ll sum, ll n, ll now, ll jnum);
ll joi[65005] = {};

int main() {
  ll n, p;
  while (1) {
    p = 0;
    scanf("%lld", &n);
    if (n == 0)
      break;
    for (ll i = 0; i < n; i++) {
      ll a;
      scanf("%lld", &a);
      p += a;
    }
    for (ll i = 0; i < n - 1; i++) {
      scanf("%lld", &joi[i]);
    }
    sort(joi, joi + n - 1);
    cout << ans(p, n, 0, n - 2) << endl;
  }
  return 0;
}

ll ans(ll sum, ll n, ll now, ll jnum) {
  ll aa = sum * n, es;
  if (aa < (sum + joi[jnum - now]) * (n - 1)) {
    es = ans(sum + joi[jnum - now], n - 1, now + 1, jnum);
    aa = max(es, aa);
  }
  return aa;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int ans(int sum, int n, int now, int jnum);
int joi[65005];

int main() {
  int n, p;
  while (1) {
    p = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      p += a;
    }
    for (int i = 0; i < n - 1; i++) {
      scanf("%d", &joi[i]);
    }
    cout << ans(p, n, 0, n - 1) << endl;
  }
  return 0;
}

int ans(int sum, int n, int now, int jnum) {
  int aa = sum * n, es;
  for (int i = now; i < jnum; i++) {
    if (sum * n < (sum + joi[i]) * (n - 1)) {
      es = ans(sum + joi[i], n - 1, i + 1, jnum);
      aa = max(es, aa);
    }
  }
  return aa;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

int main() {
  int n;
  while (cin >> n, n) {
    vector<unsigned int> S;
    unsigned int mx = 0, a;
    unsigned int sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> a;
      sum += a;
    }
    S.push_back(0);
    for (int i = 0; i < n - 1; i++) {
      cin >> a;
      S.push_back(a);
    }
    sort(S.begin() + 1, S.end(), greater<int>());

    for (int i = 0; i < n; i++) {
      sum += S[i];
      mx = max(mx, sum * (n - i));
    }

    cout << mx << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> J;
    int mx = 0, a;
    int pipe = 0;
    for (int i = 0; i < n; i++) {
      cin >> a;
      pipe += a;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> a;
      J.push_back(a);
    }

    sort(J.begin(), J.end(), greater<int>());

    for (int i = 0; i <= J.size(); i++) {
      int sum = pipe;
      for (int j = 0; j < i; j++) {
        sum += J[j];
      }
      sum *= (n - i);

      mx = max(mx, sum);
    }

    cout << mx << endl;
  }
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
int n;
int joint[70000];
long long sum;

void solve() {
  long long ans = sum * n;
  int num = n;
  long long joint_sum = 0;
  rep(i, n - 1) {
    joint_sum += joint[i];
    ans = max(ans, (sum + joint_sum) * (n - i - 1));
  }
  cout << ans << endl;
}

int main() {
  while (cin >> n, n) {
    sum = 0;
    rep(i, n) {
      int p;
      cin >> p;
      sum += p;
    }
    rep(i, n - 1) { cin >> joint[i]; }
    sort(joint, joint + (n - 1), greater<int>());
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
int n;
long long p[70000];
long long joint[70000];
long long dp[2][70000];
void solve() {
  dp[0][1] = p[0];
  rep(i, n - 1) {
    // i番目を使うかどうか
    REP(j, 1, i + 2) {
      // 使わない
      dp[1][j + 1] = max(dp[0][j] + p[i + 1], dp[1][j + 1]);
      // 使う
      dp[1][j] = max(dp[0][j] + p[i + 1] + joint[i], dp[1][j]);
    }
    REP(j, 1, i + 3) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }
  long long ans = 0;
  REP(i, 1, n + 1) { ans = max(ans, dp[0][i] * i); }
  cout << ans << endl;
}
int main() {
  while (cin >> n, n) {
    memset(dp, 0, sizeof(dp));
    rep(i, n) { cin >> p[i]; }
    rep(i, n - 1) { cin >> joint[i]; }
    solve();
  }
  return 0;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

int main(void) {
  int n, i, e, h[65000];
  unsigned int x, z, a, w;
  while (1) {
    w = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &e);
      w += e;
    }
    for (i = 0; i < n - 1; i++) {
      scanf("%d", &h[i]);
    }
    sort(h, h + n - 1, greater<int>());
    x = n * w;
    z = w;
    for (i = 0; i < n - 1; i++) {
      z = z + h[i];
      a = (n - (i + 1)) * z;
      if (a > x) {
        x = a;
      }
    }
    printf("%u\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

int main(void) {
  int n, i, e, h[10000];
  unsigned int x, z, a, w;
  while (1) {
    w = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d", &e);
      w += e;
    }
    for (i = 0; i < n - 1; i++) {
      scanf("%d", &h[i]);
    }
    sort(h, h + n - 1, greater<int>());
    x = n * w;
    z = w;
    for (i = 0; i < n - 1; i++) {
      z = z + h[i];
      a = (n - (i + 1)) * z;
      if (a > x) {
        x = a;
      }
    }
    printf("%u\\n", x);
  }
  return 0;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int x[150000], y[150000], n;
long long sum, maxn;
int main() {
  while (true) {
    sum = 0;
    maxn = 0;
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      sum += x[i];
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> y[i];
      sum += y[i];
    }
    sort(y, y + n - 1);
    maxn = sum;
    for (int i = 0; i < n - 1; i++) {
      sum -= y[i];
      maxn = max(maxn, sum * (i + 2));
    }
    cout << maxn << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long sum, k, a[130000], n, pass;
int main() {
  while (true) {
    sum = 0;
    k = 0;
    pass = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i * 2];
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> a[i * 2 + 1];
    }
    while (true) {
      if (a[pass] >= a[pass + 1] || pass >= n * 2 - 4) {
        sum += a[pass];
        k++;
        pass += 2;
      } else {
        sum += a[pass] + a[pass + 1] + a[pass + 2];
        k++;
        pass += 4;
      }
      if (pass > n * 2 - 2) {
        break;
      }
    }
    cout << sum * k << endl;
  }
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

    unsigned int p = 0, ptmp;
    int j[65000];

    for (int i = 0; i < n; i++) {
      scanf("%d", &ptmp);
      p += ptmp;
    }
    for (int i = 0; i < n - 1; i++)
      scanf("%d", &j[i]);

    sort(j, j + n - 1, greater<int>());

    int ntmp = n;

    for (int i = 0; i < n - 1; i++) {
      if (p * ntmp <= (p + j[i]) * (ntmp - 1)) {
        ntmp--;
        p += j[i];
      } else {
        break;
      }
    }
    cout << p * ntmp << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int p[65000];
int j[65000];
int n;

int dfs(int cnt, int sum, int kosu) {
  if (cnt == n)
    return sum * kosu;
  else {
    int rec1 = dfs(cnt + 1, sum + j[cnt], kosu - 1);
    int rec2 = dfs(cnt + 1, sum, kosu);
    return max(rec1, rec2);
  }
}

int main() {
  ios_base::sync_with_stdio(false);

  int sum;

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

    sum = 0;

    for (int i = 0; i < n; ++i) {
      cin >> p[i];
      sum += p[i];
    }
    for (int i = 0; i < n - 1; ++i) {
      cin >> j[i];
    }

    cout << dfs(0, sum, n) << endl;
  }
  return 0;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

//-- ソート --//

void f_sort(int[], int);

int main(void) {
  // パイプの本数
  int h_paipu[65000] = {0};
  int h_zyoint[65000] = {0};

  while (true) {
    // 入力変数
    int n = 0;
    // 2数(貪欲方で使う)
    unsigned int no = 0;
    unsigned int yes = 0;

    cin >> n;

    if (n == 0) {
      return 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> h_paipu[i];
    }

    // ジョイントのインデックス
    int zyoint_index = n - 1;

    for (int i = 0; i < zyoint_index; i++) {
      cin >> h_zyoint[i];
    }

    // ジョイントをソート
    f_sort(h_zyoint, zyoint_index);

    int count = 0;

    while (yes >= no && n > 1) {
      no = 0;
      yes = 0;

      // ジョイントをつなげなかった場合
      for (int i = 0; i < n; i++) {
        no += h_paipu[i];
      }
      no *= n;

      // パイプを繋げる
      h_paipu[n - 2] += h_paipu[n - 1];
      // ジョイントもつなげる
      h_paipu[n - 2] += h_zyoint[count++];
      // 1本減ってしまった
      n--;

      // forで回して計算する
      for (int i = 0; i < n; i++) {
        yes += h_paipu[i];
      }
      yes *= n;
    }

    if (yes > no) {
      cout << yes << endl;
    } else {
      cout << no << endl;
    }
  }

  return 0;
}

//*****----- ソート -----*****//

void f_sort(int h_zyoint[], int n) {
  sort(h_zyoint, h_zyoint + n);

  for (int i = 0; i < n / 2; i++) {
    int tmp = h_zyoint[i];
    h_zyoint[i] = h_zyoint[n - i - 1];
    h_zyoint[n - i - 1] = tmp;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

//-- ソート --//

void f_sort(int[], int);

int main(void) {
  // パイプの本数
  int h_paipu[65000] = {0};
  int h_zyoint[65000] = {0};

  while (true) {
    // 入力変数
    int n = 0;
    // 2数(貪欲方で使う)
    unsigned int no = 0;
    unsigned int yes = 0;

    cin >> n;

    if (n == 0) {
      return 0;
    }

    for (int i = 0; i < n; i++) {
      cin >> h_paipu[i];
    }

    // ジョイントのインデックス
    int zyoint_index = n - 1;

    for (int i = 0; i < zyoint_index; i++) {
      cin >> h_zyoint[i];
    }

    // ジョイントをソート
    f_sort(h_zyoint, zyoint_index);

    int count = 0;

    while (yes >= no && n > 1) {
      no = 0;
      yes = 0;

      // ジョイントをつなげなかった場合
      for (int i = 0; i < n; i++) {
        no += h_paipu[i];
      }
      no *= n;

      // パイプを繋げる
      h_paipu[n - 2] += h_paipu[n - 1];
      // ジョイントもつなげる
      h_paipu[n - 2] += h_zyoint[count++];
      // 1本減ってしまった
      n--;

      // forで回して計算する
      for (int i = 0; i < n; i++) {
        yes += h_paipu[i];
      }
      yes *= n;
    }

    if (yes > no) {
      cout << yes << endl;
    } else {
      cout << no << endl;
    }
  }

  return 0;
}

//*****----- ソート -----*****//

void f_sort(int h_zyoint[], int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (h_zyoint[i] < h_zyoint[j]) {
        int tmp = h_zyoint[i];
        h_zyoint[i] = h_zyoint[j];
        h_zyoint[j] = tmp;
      }
    }
  }
}
Problem p00255 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

typedef long long int ll;
int main() {
  ll n, array[65001], p[65001];
  while (cin >> n && n) {
    ll s = 0;
    for (int i = 0; i < n; i++) {
      cin >> array[i];
      s += array[i];
    }

    for (int i = 0; i < n - 1; i++)
      cin >> p[i];

    ll ans = 0;
    sort(p, p + n - 1);
    reverse(p, p + n - 1);

    for (int i = 0; i < n; i++)
      ans += array[i];
    ans *= n;

    ll pipe = 0;
    for (int i = 1; i < n; i++) {
      pipe += p[i - 1];
      ans = max(ans, (ll)((pipe + s) * (n - i)));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, array[101], p[101];
  while (cin >> n && n) {
    int s = 0;
    for (int i = 0; i < n; i++) {
      cin >> array[i];
      s += array[i];
    }

    for (int i = 0; i < n - 1; i++)
      cin >> p[i];

    int ans = 0;
    sort(array, array + n);
    sort(p, p + n - 1);
    reverse(p, p + n - 1);

    for (int i = 0; i < n; i++) {
      ans += array[i];
    }
    ans *= n;

    int sum = array[0], pipe = 0;
    for (int i = 1; i < n; i++) {
      sum += array[i];
      pipe += p[i - 1];
      ans = max(ans, (sum + pipe + (s - sum)) * (n - i));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00255 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

long long joint[65500];

int main(void) {
  long long n;
  while (cin >> n, n) {
    if (n == 0) {
      return 0;
    }

    long long sum = 0, a;
    for (int i = 0; i < n; i++) {
      cin >> a;
      sum += a;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> joint[i];
      sum += joint[i];
    }
    sort(joint, joint + n - 1);

    long long ret = sum;
    for (int i = 0; i < n - 1; i++) {
      sum -= joint[i];
      ret = max(ret, sum * (i + 2));
    }
    cout << ret << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {
  long long n;
  while (cin >> n, n) {
    if (n == 0) {
      return 0;
    }
    long long joint[1028];
    long long sum = 0, a;
    for (long long i = 0; i < n; i++) {
      cin >> a;
      sum += a;
    }
    for (long long i = 0; i < n - 1; i++) {
      cin >> joint[i];
      sum += joint[i];
    }
    sort(joint, joint + n - 1);

    long long ret = sum;
    for (long long i = 0; i < n - 1; i++) {
      sum -= joint[i];
      ret = max(ret, sum * (i + 2));
    }
    cout << ret << endl;
  }

  return 0;
}
Problem p00256 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef unsigned int64;
#define __2012_12_21__ 735224
#define MAX_YEAR 10000000

int64 days[MAX_YEAR + 1];
int month[2][13] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

bool urudosi(int year) {
  return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}
void init_set() {
  for (int i = 1; i <= MAX_YEAR; i++) {
    days[i] = days[i - 1] + 365 + urudosi(i - 1);
  }
}

int main() {
  char buff[256];
  int y, m, d;         // 西暦
  int b, ka, t, w, ki; // マヤ長期歴

  init_set();
  while (strcmp(fgets(buff, sizeof(buff), stdin), "#\\n") != 0) {

    /* 入力 */
    if (sscanf(buff, "%d.%d.%d.%d.%d", &b, &ka, &t, &w, &ki) == 5) {
      /*マヤ長期歴 → 西暦 */
      int64 allday =
          b * 144000 + ka * 7200 + t * 360 + w * 20 + ki + __2012_12_21__;
      y = lower_bound(days, days + MAX_YEAR, allday) - days - 1;
      allday -= days[y];
      bool uru = urudosi(y);
      m = lower_bound(month[uru], month[uru] + 13, allday) - month[uru];
      d = allday - month[uru][m - 1];
      printf("%d.%d.%d\\n", y, m, d);
    } else {
      sscanf(buff, "%d.%d.%d", &y, &m, &d);
      int64 allday = days[y] + month[urudosi(y)][m - 1] + d - __2012_12_21__;
      ki = allday % 20;
      w = allday / 20 % 18;
      t = allday / 360 % 20;
      ka = allday / 7200 % 20;
      b = allday / 144000 % 13;
      printf("%d.%d.%d.%d.%d\\n", b, ka, t, w, ki);
    }
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
#define __2012_12_21__ 735224
#define MAX_YEAR 10000000

int64 days[MAX_YEAR + 1];
int month[2][13] = {
    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

bool urudosi(int year) {
  return year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}
void init_set() {
  for (int i = 1; i <= MAX_YEAR; i++) {
    days[i] = days[i - 1] + 365 + urudosi(i - 1);
  }
}

int main() {
  char buff[256];
  int y, m, d;         // 西暦
  int b, ka, t, w, ki; // マヤ長期歴

  init_set();
  while (strcmp(fgets(buff, sizeof(buff), stdin), "#\\n") != 0) {

    /* 入力 */
    if (sscanf(buff, "%d.%d.%d.%d.%d", &b, &ka, &t, &w, &ki) == 5) {
      /*マヤ長期歴 → 西暦 */
      int64 allday =
          b * 144000 + ka * 7200 + t * 360 + w * 20 + ki + __2012_12_21__;
      y = lower_bound(days, days + MAX_YEAR, allday) - days - 1;
      allday -= days[y];
      bool uru = urudosi(y);
      m = lower_bound(month[uru], month[uru] + 13, allday) - month[uru];
      d = allday - month[uru][m - 1];
      printf("%d.%d.%d\\n", y, m, d);
    } else {
      sscanf(buff, "%d.%d.%d", &y, &m, &d);
      int64 allday = days[y] + month[urudosi(y)][m - 1] + d - __2012_12_21__;
      ki = allday % 20;
      w = allday / 20 % 18;
      t = allday / 360 % 20;
      ka = allday / 7200 % 20;
      b = allday / 144000 % 13;
      printf("%d.%d.%d.%d.%d\\n", b, ka, t, w, ki);
    }
  }
}
Problem p00257 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()

using namespace std;

typedef vector<int> vi;

int m, n;
vi ok;
int dist[300][300];

int main() {
  // freopen("test.txt", "r", stdin);
  while (cin >> m, m) {
    cin >> n;
    vi d(n + 2, 0);
    FOR(i, 1, n + 1) cin >> d[i];
    rep(i, n + 2) {
      d[i] += i;
      if (d[i] < 0)
        d[i] = 0;
      if (d[i] > n)
        d[i] = n + 1;
    }

    const int INF = 10000;
    rep(i, n + 2) rep(j, n + 2) dist[i][j] = INF;
    dist[0][0] = 0;

    rep(i, n + 2) {
      for (int j = 1; j <= m; j++) {
        int dest = i + j;
        if (dest > n)
          dest = n + 1;
        dist[i][d[dest]] = min(dist[i][d[dest]], 1);
      }
    }
    rep(i, n + 2) rep(j, n + 2) rep(k, n + 2) dist[j][k] =
        min(dist[j][k], dist[j][i] + dist[i][k]);

    bool ok = true;
    rep(i, n + 2) { ok &= (dist[0][i] == INF || dist[i][n + 1] != INF); }
    cout << (ok ? "OK" : "NG") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()

using namespace std;

typedef vector<int> vi;

int m, n;
vi ok;
vi d;

bool isok(int num) {
  if (ok[num] != -1)
    return ok[num];
  for (int i = m; i >= 1; i--) {
    int next = num + i;
    if (next < 0)
      next = 0;
    if (next > n)
      return ok[num] = true;
    next += d[next];
    if (next < 0)
      next = 0;
    if (next == num)
      continue;
    if (next > n)
      return ok[num] = true;
    if (isok(next))
      return ok[num] = true;
  }

  return false;
}

int main() {
  // freopen("test.txt", "r", stdin);
  while (cin >> m, m) {
    cin >> n;
    d.resize(n + 1);
    fill(all(d), 0);
    rep(i, n) cin >> d[i + 1];
    ok.resize(n + 1);
    fill(all(ok), -1);

    bool ans = true;
    rep(i, n) ans &= isok(i);

    cout << (ans ? "OK" : "NG") << endl;
  }

  return 0;
}
Problem p00257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  while (cin >> m, m) {
    cin >> n;
    int d[300] = {};
    int i;
    int p, b;
    fill(d, d + 300, 0);
    for (i = 1; i <= n; i++)
      cin >> d[i];
    int used[300];
    bool r[300] = {};
    bool g[300] = {};
    queue<int> q1, q2;
    q1.push(0);
    bool c = true;
    while (!q1.empty()) {
      b = q1.front();
      q1.pop();
      if (r[b])
        continue;
      r[b] = true;
      q2.push(b);
      // cout << b << endl;
      fill(used, used + 300, false);
      while (!q2.empty()) {
        p = q2.front();
        q2.pop();
        // cout << p << endl;
        if (p < 0)
          p = 0;
        if (p > n + 1)
          p = n + 1;
        if (used[p])
          continue;
        used[p] = true;
        q1.push(p);
        for (i = 1; i <= m; i++) {
          // cout <<p<<":"<< i <<":"<<p+i+d[p+i]<< endl;
          q2.push(p + i + d[p + i]);
        }
      }
      if (!used[n + 1]) {
        c = false;
        break;
      }
    }
    if (c)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  while (cin >> m, m) {
    cin >> n;
    int d[300] = {}, o[300] = {};
    int i;
    int p, b;
    for (i = 1; i <= n; i++)
      cin >> d[i];
    int used[300];
    for (i = 1; i <= n; i++) {
      p = i;
      while (d[p] != 0) {
        p += d[p];
        if (p < 0)
          p = 0;
        if (p > n + 1)
          p = n + 1;
      }
      o[i] = p;
    }
    o[n + 1] = n + 1;
    fill(used, used + 300, false);

    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      p = q.front();
      q.pop();
      // cout << p << endl;
      p = o[p];
      if (used[p])
        continue;
      used[p] = true;
      if (used[n + 1])
        break;
      for (i = 1; i <= m; i++) {
        if (p + i > n + 1)
          q.push(n + 1);
        else
          q.push(p + i);
      }
    }
    if (used[n + 1])
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}