Problem p00467 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define sz size()
#define lgt length()
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define pb push_back
#define SORT(a) sort(ALL(a));
#define RSORT(a) sort(RALL(a));
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
#define fir first
#define sec second

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
vs form(string a, char s) {
  vs v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(b);
      b = "";
    } else
      b += a[i];
  }
  v.pb(b);
  return v;
}
vi formint(string a, char s) {
  vi v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(toInt(b));
      b = "";
    } else
      b += a[i];
  }
  v.pb(toInt(b));
  return v;
}

const double EPS = 1e-10, PI = acos(-1.0);
const int INF = 2100000000;

int field[1024];

int main(void) {
  int n, m, tmp;
  while (cin >> n >> m, n && m) {
    for (int i = 1; i <= n; i++)
      cin >> field[i];
    int now = 1, res = 0;
    bool flag = false;

    for (int i = 0; i < m; i++) {
      cin >> tmp;
      if (!flag)
        res++;
      if (!flag)
        now += tmp;
      if (n <= now)
        flag = true;
      if (!flag)
        now += field[now];
      if (n <= now)
        flag = true;
      if (now < 1)
        now = 1;
    }

    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define sz size()
#define lgt length()
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define pb push_back
#define SORT(a) sort(ALL(a));
#define RSORT(a) sort(RALL(a));
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
#define fir first
#define sec second

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
vs form(string a, char s) {
  vs v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(b);
      b = "";
    } else
      b += a[i];
  }
  v.pb(b);
  return v;
}
vi formint(string a, char s) {
  vi v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(toInt(b));
      b = "";
    } else
      b += a[i];
  }
  v.pb(toInt(b));
  return v;
}

const double EPS = 1e-10, PI = acos(-1.0);
const int INF = 2100000000;

int field[1024];

int main(void) {
  int n, m, tmp;
  while (cin >> n >> m, n && m) {
    for (int i = 1; i <= n; i++)
      cin >> field[i];
    int now = 1, res = 0;
    bool flag = false;

    for (int i = 0; i < m; i++) {
      cin >> tmp;
      if (!flag)
        res++;
      now += tmp;
      if (n <= now)
        flag = true;
      now += field[now];
      if (now < 1)
        now = 1;
      //			cout << now << " " << field[now] << endl;
    }

    cout << res << endl;
  }

  return 0;
}