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

#include <cstdio>
#include <iostream>
// #include <stdlib.h>
using namespace std;

#define debug 0

int InArray[200000] = {0};
int N = 0;

long long sumArray(int, int);

int main() {
  // initialize input
  int temp;
#if debug
  FILE *inputFile = fopen("0600_in1.txt", "r");
  fscanf(inputFile, "%d", &N);
  // cout << "N: " << N << endl;
  for (int i = 0; i < N; i++) {
    fscanf(inputFile, "%d", &temp);
    // cout << temp << endl;
    InArray[i] = temp;
    InArray[i + N] = temp;
  }
  cout << endl;

  // for(int i = 0; i < 2 * N; i++){
  //	cout << InArray[i] << endl;
  // }

  fclose(inputFile);
#else
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> temp;
    InArray[i] = temp;
    InArray[i + N] = temp;
  }
#endif

  // Calculate sum and average
  long long sum = sumArray(0, N);
  long long average;
  if (sum % 3 == 0) {
    average = sum / 3;
  } else {
    average = sum / 3 + 1;
  }

#if debug
  cout << "sum:\\t" << sum << endl;
  cout << "average:" << average << endl;
#endif

  long long MaxMin = 0;
  long long tempMin;
  long long a1_sum, a2_sum;
  long long average2;

  for (int a1 = 0; a1 < N; a1++) {
    a1_sum = 0;
    for (int a2 = a1 + 1; a2 < (a1 + N); a2++) {
      a1_sum += InArray[a2 - 1];
      if (a1_sum < average)
        continue;

      if ((sum - a1_sum) % 2 == 0) {
        average2 = (sum - a1_sum) / 2;
      } else {
        average2 = (sum - a1_sum + 1) / 2;
      }

      a2_sum = 0;
      for (int a3 = a2 + 1; a3 < (a1 + N); a3++) { // left 1 for a3_sum
        a2_sum += InArray[a3 - 1];
        if (a2_sum < average2)
          continue;
#if debug
// cout << a1 << " " << a2 << " " << a3 << endl;
// cout << a1_sum << " " << a2_sum << " " << sum - a1_sum - a2_sum << endl;
#endif
        if (a2_sum - InArray[a3 - 1] > MaxMin) {
          MaxMin = a2_sum - InArray[a3 - 1];
#if debug
// cout << MaxMin << endl;
#endif
        }
        if (sum - a1_sum - a2_sum > MaxMin) {
          MaxMin = sum - a1_sum - a2_sum;
#if debug
// cout << MaxMin << endl;
#endif
        }
        break;
      }
      break;
    }
  }

  cout << MaxMin << endl;

  return 0;
}

long long sumArray(int start, int end) {
  long long sum = 0;
  for (int i = start; i < end; i++) {
    sum += InArray[i];
    //		cout << "i:" << i << "sum:" << sum << endl;
  }
  //	cout << sum << endl;
  //	system("pause");
  return sum;
}

Error: -11

Fixed submission:

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

long InArray[100000] = {0};
int N = 0;

long sumArray(int, int);
long getLocalMin(int, int, int);

int main() {
  // initialize input
  FILE *inputFile = fopen("0600_in4.txt", "r");
  fscanf(inputFile, "%d", &N);
  for (int i = 0; i < N; i++) {
    fscanf(inputFile, "%d", &InArray[i]);
  }
  fclose(inputFile);

  // cin >> N;
  // for(int i = 0; i < N; i++){cin >> InArray[i];}

  // Calculate sum and average
  long sum = sumArray(0, N);
  long average = sum / 3;

  // cout << "sum:\\t" << sum << endl;
  // cout << "average:" << average << endl;

  long MaxMin = 0;
  long tempMin;
  long a1_sum, a2_sum, a3_sum;

  a1_sum = 0;
  for (int a1 = 0;; a1++) {
    a2_sum = InArray[a1];
    for (int a2 = a1 + 1;; a2++) {
      // if(sumArray(a1, a2+1) < average) continue;
      a2_sum += InArray[a2];
      if (a2_sum + InArray[a2 + 1] < average)
        continue;
      a3_sum = 0;
      for (int a3 = a2 + 1; a3 < N; a3++) {
        // if(sumArray(a2, a3+1) < average) continue;
        a3_sum += InArray[a3];
        if (a3_sum + InArray[a3 + 1] < average)
          continue;
        tempMin = getLocalMin(a1, a2, a3);
        if (MaxMin < tempMin)
          MaxMin = tempMin;
        tempMin = getLocalMin(a1, a2, a3 + 1);
        if (MaxMin < tempMin)
          MaxMin = tempMin;
        break;
      }

      a2 = a2 + 1;
      a3_sum = 0;
      for (int a3 = a2 + 1; a3 < N; a3++) {
        // if(sumArray(a2, a3+1) < average) continue;
        a3_sum += InArray[a3];
        if (a3_sum + InArray[a3 + 1] < average)
          continue;
        tempMin = getLocalMin(a1, a2, a3);
        if (MaxMin < tempMin)
          MaxMin = tempMin;
        tempMin = getLocalMin(a1, a2, a3 + 1);
        if (MaxMin < tempMin)
          MaxMin = tempMin;
        break;
      }
      break;
    }
    // if(sumArray(0, a1+1) > average) break;
    a1_sum += InArray[a1];
    if (a1_sum + InArray[a1 + 1] > average)
      break;
  }

  cout << MaxMin << endl;

  return 0;
}

long sumArray(int start, int end) {
  long sum = 0;
  for (int i = start; i < end; i++) {
    sum += InArray[i];
  }
  return sum;
}

long getLocalMin(int p1, int p2, int p3) {
  long LocalMin = sumArray(p1, p2);
  long tempSum = sumArray(p2, p3);
  if (LocalMin > tempSum) {
    LocalMin = tempSum;
  }
  tempSum = sumArray(0, p1) + sumArray(p3, N);
  if (LocalMin > tempSum) {
    LocalMin = tempSum;
  }
  // cout << "getLocalMin(a1, a2, a3)\\t" << LocalMin << "\\t" << p1 << "\\t" << p2
  // << "\\t" << p3 << endl;
  return LocalMin;
}