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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

//
//??????index??¨???????????????????????¨?????¢??????????¶??????????
// map????????£???[??????,?????°]?????¨???????????°????????¢????????????????????????
//

int main(void) {
  ll ans[1000], cnt = 0;
  rep(i, 1000) ans[i] = 0;
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int h[1501], w[1501];
    reg(i, 1, N) cin >> h[i];
    reg(i, 1, M) cin >> w[i];
    ll DP[1501];
    map<ll, int> mapH;
    map<ll, int> mapW;
    DP[0] = 0;
    rep(i, N) DP[i + 1] = DP[i] + h[i + 1]; // h???i???????????§????´???????
    reg(i, 1, N) {                          //???????????§??????
      reg(j, 0, N - i)
          mapH[DP[j + i] -
               DP[j]]++; // h?????????????????¨????????¨??????????±???????
    }
    rep(i, M) DP[i + 1] = DP[i] + w[i + 1]; // w???i???????????§????´???????
    reg(i, 1, M) {                          //???????????§??????
      reg(j, 0, M - i)
          mapW[DP[j + i] -
               DP[j]]++; // w?????????????????¨????????¨??????????±???????
    }

    for (map<ll, int>::iterator p = mapH.begin(); p != mapH.end(); ++p) {
      ll key = p->first;
      ans[cnt] += mapW[key] * mapH[key];
    }
    cnt++;
  }
  rep(i, cnt) cout << ans[i] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int main(void) {
  ll ans[1000], cnt = 0;
  rep(i, 1000) ans[i] = 0;
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    int h[1501], w[1501];
    reg(i, 1, N) cin >> h[i];
    reg(i, 1, M) cin >> w[i];
    ll DP[1501], max; // i???????????§????´???????
    DP[0] = 0;
    rep(i, N) DP[i + 1] = DP[i] + w[i + 1];
    max = DP[N];
    int mapW[max + 1];
    rep(i, max + 1) mapW[i] = 0;
    reg(i, 1, N) { //??§??????
      reg(j, 0, N - i) mapW[DP[j + i] - DP[j]]++;
    }
    rep(i, M) DP[i + 1] = DP[i] + h[i + 1];
    if (max < DP[M])
      max = DP[M];
    int mapH[max + 1];
    rep(i, max + 1) mapH[i] = 0;
    reg(i, 1, M) { //??§??????
      reg(j, 0, M - i) mapH[DP[j + i] - DP[j]]++;
    }
    rep(i, max + 1) { ans[cnt] += mapW[i] * mapH[i]; }
    cnt++;
  }
  rep(i, cnt) cout << ans[i] << endl;
  return 0;
}