Problem p02623 (C++)

Buggy submission (status: Runtime Error):
#define DEBUG 1
#include <bits/stdc++.h>
using namespace std;
typedef char s8;
typedef unsigned char u8;
typedef short s16;
typedef unsigned short u16;
typedef int s32;
typedef unsigned int u32;
typedef long int s64;
typedef unsigned long int u64;
typedef float f32;
typedef double f64;
#define rep(_i, _n) for (int _i = 0; _i < (int)(_n); _i++)
#if DEBUG
#define PHERE()                                                                \
  { cerr << "L" << setw(3) << __LINE__ << ": PHERE" << clock() << endl; }
#define PVAL(_var)                                                             \
  { cerr << "L" << setw(3) << __LINE__ << ":" << #_var "=" << (_var) << endl; }
#else
#define PHERE()
#define PVAL(_var)
#endif

int main() {
  //////// 入力 ////////
  s64 N, M, K;
  cin >> N >> M >> K;

  s64 ASum[N];
  s64 AMax = -1;
  rep(i, N) {
    cin >> ASum[i];
    if (i > 0) {
      ASum[i] += ASum[i - 1];
    }
    if (AMax < 0) {
      if (ASum[i] > K) {
        AMax = i;
      }
    }
  }
  if (AMax < 0) {
    AMax = N;
  }
  s64 BSum[N];
  s64 BMax = -1;
  rep(i, M) {
    cin >> BSum[i];
    if (i > 0) {
      BSum[i] += BSum[i - 1];
    }
    if (BMax < 0) {
      if (BSum[i] > K) {
        BMax = i;
      }
    }
  }
  if (BMax < 0) {
    BMax = M;
  }

  /////////////////////

  s64 outMax = max(AMax, BMax);
  // DEL 	for( s64 i = AMax-1; i >= 0; --i ) {
  for (s64 i = -1; i < AMax; ++i) {
    s64 posMin = 0;
    s64 posMax = BMax;
    s64 find = -1;
    s64 curA = 0;
    if (i >= 0) {
      curA = ASum[i];
    }
    while (1) {
      s64 bPos = (posMin + posMax) / 2;
      if (curA + BSum[bPos] <= K) {
        posMin = bPos;
      } else {
        posMax = bPos;
      }
      if (posMax - posMin <= 1) {
        if (curA + BSum[posMin] <= K) {
          find = posMin;
        }
        break;
      }
    }
    if (find >= 0) {
      if (i + find + 2 > outMax) {
        outMax = i + find + 2;
      }
    }
#if 0
		for( s64 j = max(0,outMax - i - 1); j < BMax; ++j ) {
			if( ASum[i] + BSum[j] <= K ) {
				outMax = i+j+2;
			}
		}
#endif
  }

  //////// 出力 ////////
  cout << outMax;
  /////////////////////

  return 0;
}
#if 0
#endif


Fixed submission:
#define DEBUG 1
#include <bits/stdc++.h>
using namespace std;
typedef char s8;
typedef unsigned char u8;
typedef short s16;
typedef unsigned short u16;
typedef int s32;
typedef unsigned int u32;
typedef long int s64;
typedef unsigned long int u64;
typedef float f32;
typedef double f64;
#define rep(_i, _n) for (int _i = 0; _i < (int)(_n); _i++)
#if DEBUG
#define PHERE()                                                                \
  { cerr << "L" << setw(3) << __LINE__ << ": PHERE" << clock() << endl; }
#define PVAL(_var)                                                             \
  { cerr << "L" << setw(3) << __LINE__ << ":" << #_var "=" << (_var) << endl; }
#else
#define PHERE()
#define PVAL(_var)
#endif

int main() {
  //////// 入力 ////////
  s64 N, M, K;
  cin >> N >> M >> K;

  s64 ASum[N];
  s64 AMax = -1;
  rep(i, N) {
    cin >> ASum[i];
    if (i > 0) {
      ASum[i] += ASum[i - 1];
    }
    if (AMax < 0) {
      if (ASum[i] > K) {
        AMax = i;
      }
    }
  }
  if (AMax < 0) {
    AMax = N;
  }
  s64 BSum[M];
  s64 BMax = -1;
  rep(i, M) {
    cin >> BSum[i];
    if (i > 0) {
      BSum[i] += BSum[i - 1];
    }
    if (BMax < 0) {
      if (BSum[i] > K) {
        BMax = i;
      }
    }
  }
  if (BMax < 0) {
    BMax = M;
  }

  /////////////////////

  s64 outMax = max(AMax, BMax);
  // DEL 	for( s64 i = AMax-1; i >= 0; --i ) {
  for (s64 i = -1; i < AMax; ++i) {
    s64 posMin = 0;
    s64 posMax = BMax;
    s64 find = -1;
    s64 curA = 0;
    if (i >= 0) {
      curA = ASum[i];
    }
    while (1) {
      s64 bPos = (posMin + posMax) / 2;
      if (curA + BSum[bPos] <= K) {
        posMin = bPos;
      } else {
        posMax = bPos;
      }
      if (posMax - posMin <= 1) {
        if (curA + BSum[posMin] <= K) {
          find = posMin;
        }
        break;
      }
    }
    if (find >= 0) {
      if (i + find + 2 > outMax) {
        outMax = i + find + 2;
      }
    }
#if 0
		for( s64 j = max(0,outMax - i - 1); j < BMax; ++j ) {
			if( ASum[i] + BSum[j] <= K ) {
				outMax = i+j+2;
			}
		}
#endif
  }

  //////// 出力 ////////
  cout << outMax;
  /////////////////////

  return 0;
}
#if 0
#endif