public void solve() throws IOException {
    int n = nextInt();
    int[] a = new int[n];
    long total = 0;
    for (int x = 0; x < n; x++) {
      a[x] = nextInt();
      total += a[x];
    }

    if (total % 3 != 0) {
      System.out.println(0);
      return;
    }

    long third = total / 3;
    long twothird = 2 * third;

    long first = 0;
    long tot = 0;

    long[] partials = new long[n];
    for (int x = 0; x < n - 1; x++) {
      if (x == 0) {
        partials[x] = a[x];
      } else {
        partials[x] = partials[x - 1] + a[x];
      }

      if (partials[x] == twothird) {
        tot += first;
      }
      if (partials[x] == third) {
        first++;
      }
      // System.out.println(partials[x] + " " + first.size() + " " +
      // second.size());
    }
    System.out.println(tot);
  }