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

/*
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015
?????????: Square Route
?????°:
18:51:30 ??????????????????
18:54:38 ??????????????¨
19:17:56 ??¨???????????????????????????
19:18:43 ?????°???
19:30:54 ?????°?????????????°???\\???
19:31:54 Memory Limit Exceeded?????¨??????
19:33:37 ??¨?????????????????????????????????
?¢??????????????????????????????°?????°????????°?????????????????????????????????????????¨?????????????????????
19:45:01 ???????????????
19:46:02
????????¨?????????????????£???????????????????????§????????¢????????????????????????
19:48:07
???????????????????????????multiset???????????£????????????map???????????????map??????????????????1000????´????
19:53:28 ????????§????????????
19:54:37 ???????????¨???????????¨???
19:55:06 ????????????????´??????????????????????
19:57:49 ?????????????????¨??????
19:58:46 ??????????????°???????????¨?????????
*/

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
using namespace std;
#define ws WS //??????????????????????????§??????

int h[1500];
int w[1500];

// hsum[0] = 0;
// hsum[1] = h[0];
// hsum[2] = h[0] + h[1];
// hsum[3] = h[0] + h[1] + h[2];
int hsum[1501];
int wsum[1501];

// orig????´???????????±???????sum?????\\?????????
void foo(int orig[], int sum[], int length) {
  int tmp = 0;
  for (int i = 0; i < length; i++) {
    sum[i] = tmp;
    tmp += orig[i];
  }
  sum[length] = tmp;
}

#define AA 1500000
int hs[AA]; // hs[???] = ???????????°
int ws[AA]; // ws[???] = ???????????°

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }
    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }

    foo(h, hsum, N);
    foo(w, wsum, M);

    for (int i = 0; i < AA; i++) {
      hs[i] = ws[i] = 0;
    }

    // ?????£?¶???????h???????????¨?????£?¶???????w?????????????????´????????¨???????????£?????¢???????????????
    // hsum[a] - hsum[b] = wsum[c] - wsum[d]??????(a>b, c>d)
    // ????´????????????????????????????????????§????????£??????????????????????????????????????????????????????????????????????????????
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        hs[hsum[j] - hsum[i]]++;
      }
    }

    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        ws[wsum[j] - wsum[i]]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < AA; i++) {
      ans += hs[i] * ws[i];
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

/*
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2015
?????????: Square Route
?????°:
18:51:30 ??????????????????
18:54:38 ??????????????¨
19:17:56 ??¨???????????????????????????
19:18:43 ?????°???
19:30:54 ?????°?????????????°???\\???
19:31:54 Memory Limit Exceeded?????¨??????
19:33:37 ??¨?????????????????????????????????
?¢??????????????????????????????°?????°????????°?????????????????????????????????????????¨?????????????????????
19:45:01 ???????????????
19:46:02
????????¨?????????????????£???????????????????????§????????¢????????????????????????
19:48:07
???????????????????????????multiset???????????£????????????map???????????????map??????????????????1000????´????
19:53:28 ????????§????????????
19:54:37 ???????????¨???????????¨???
19:55:06 ????????????????´??????????????????????
*/

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
using namespace std;
#define ws WS //??????????????????????????§??????

int h[1000];
int w[1000];

// hsum[0] = 0;
// hsum[1] = h[0];
// hsum[2] = h[0] + h[1];
// hsum[3] = h[0] + h[1] + h[2];
int hsum[1001];
int wsum[1001];

// orig????´???????????±???????sum?????\\?????????
void foo(int orig[], int sum[], int length) {
  int tmp = 0;
  for (int i = 0; i < length; i++) {
    sum[i] = tmp;
    tmp += orig[i];
  }
  sum[length] = tmp;
}

#define AA 1500000
int hs[AA]; // hs[???] = ???????????°
int ws[AA]; // ws[???] = ???????????°

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> h[i];
    }
    for (int j = 0; j < M; j++) {
      cin >> w[j];
    }

    foo(h, hsum, N);
    foo(w, wsum, M);

    for (int i = 0; i < AA; i++) {
      hs[i] = ws[i] = 0;
    }

    // ?????£?¶???????h???????????¨?????£?¶???????w?????????????????´????????¨???????????£?????¢???????????????
    // hsum[a] - hsum[b] = wsum[c] - wsum[d]??????(a>b, c>d)
    // ????´????????????????????????????????????§????????£??????????????????????????????????????????????????????????????????????????????
    for (int i = 0; i <= N; i++) {
      for (int j = i + 1; j <= N; j++) {
        hs[hsum[j] - hsum[i]]++;
      }
    }

    for (int i = 0; i <= M; i++) {
      for (int j = i + 1; j <= M; j++) {
        ws[wsum[j] - wsum[i]]++;
      }
    }

    int ans = 0;
    for (int i = 0; i < AA; i++) {
      ans += hs[i] * ws[i];
    }

    cout << ans << endl;
  }
}