#include <algorithm>
#include <bits/stdc++.h>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <vector>

// lib/monoid.hpp
#ifndef MONOID_HPP
#define MONOID_HPP


namespace tools {
  template <typename T>
  class monoid {
  private:
    ::std::function<T(T, T)> m_operation;
    T m_identity;

  public:
    monoid(const ::std::function<T(T, T)>& operation, const T& identity) :
      m_operation(operation),
      m_identity(identity) {
    }
    monoid(const ::tools::monoid<T>&) = default;
    monoid(::tools::monoid<T>&&) = default;
    ::tools::monoid<T>& operator=(const ::tools::monoid<T>&) = default;
    ::tools::monoid<T>& operator=(::tools::monoid<T>&&) = default;
    virtual ~monoid() = default;

    const ::std::function<T(T, T)>& operation() const {
      return this->m_operation;
    }
    const T& identity() const {
      return this->m_identity;
    }

    template <typename U = T, typename ::std::enable_if<::std::is_integral<U>::value, ::std::nullptr_t>::type = nullptr>
    static ::tools::monoid<U> multiplies() {
      return ::tools::monoid<U>(::std::multiplies<U>(), 1);
    }
  };
}

#endif

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// lib/group.hpp
#ifndef GROUP_HPP
#define GROUP_HPP


namespace tools {
  template <typename T>
  class group : public ::tools::monoid<T> {
  private:
    ::std::function<T(T)> m_inverse;

  public:
    group(const ::std::function<T(T, T)>& operation, const T& identity, const ::std::function<T(T)>& inverse) :
      ::tools::monoid<T>(operation, identity), m_inverse(inverse) {
    }
    group(const ::tools::group<T>&) = default;
    group(::tools::group<T>&&) = default;
    ::tools::group<T>& operator=(const ::tools::group<T>&) = default;
    ::tools::group<T>& operator=(::tools::group<T>&&) = default;
    virtual ~group() = default;

    const ::std::function<T(T)>& inverse() const {
      return this->m_inverse;
    }

    template <typename U = T, typename ::std::enable_if<::std::is_integral<U>::value, ::std::nullptr_t>::type = nullptr>
    static ::tools::group<U> plus() {
      return ::tools::group<U>(::std::plus<U>(), 0, ::std::negate<U>());
    }
  };
}

#endif

// lib/cumsum.hpp
#ifndef CUMSUM_HPP
#define CUMSUM_HPP


/*
usage:

std::vector<std::vector<int>> a;
const auto cumsum = tools::cumsum2d(tools::group<int>::plus(), a.begin(), a.end());
cumsum(x1, y1, x2, y2);
*/

namespace tools {
  namespace detail {
    namespace cumsum {
      template <typename Iterator>
      class monoid1d {
      private:
        using T = typename ::std::iterator_traits<Iterator>::value_type;
        ::tools::monoid<T> monoid;
        ::std::vector<T> from_left;
        ::std::vector<T> from_right;

      public:
        monoid1d(const ::tools::monoid<T>& monoid, const Iterator& begin, const Iterator& end) :
          monoid(monoid), from_left({monoid.identity()}), from_right({monoid.identity()}) {
          ::std::vector<T> source;
          ::std::copy(begin, end, ::std::back_inserter(source));
          this->from_left.reserve(source.size() + 1);
          for (const T& source_i : source) {
            this->from_left.push_back(monoid.operation()(this->from_left.back(), source_i));
          }
          ::std::reverse(source.begin(), source.end());
          this->from_right.reserve(source.size() + 1);
          for (const T& source_i : source) {
            this->from_right.push_back(monoid.operation()(this->from_right.back(), source_i));
          }
          ::std::reverse(this->from_right.begin(), this->from_right.end());
        }

        T operator()(const ::std::size_t l, const ::std::size_t r) const {
          if (l == 0) {
            return this->from_left[r];
          } else if (r + 1 == this->from_right.size()) {
            return this->from_right[l];
          } else {
            throw ::std::runtime_error("Unsupported operation.");
          }
        }
      };

      template <typename Iterator>
      class group1d {
      private:
        using T = typename ::std::iterator_traits<Iterator>::value_type;
        ::tools::group<T> group;
        ::std::vector<T> preprocessed;

      public:
        group1d(const ::tools::group<T>& group, const Iterator& begin, const Iterator& end) :
          group(group), preprocessed({group.identity()}) {
          for (Iterator it = begin; it != end; ++it) {
            this->preprocessed.push_back(group.operation()(this->preprocessed.back(), *it));
          }
        }

        T operator()(const ::std::size_t l, const ::std::size_t r) const {
          return this->group.operation()(this->preprocessed[r], this->group.inverse()(this->preprocessed[l]));
        }
      };

      template <typename Iterator>
      class group2d {
      private:
        using OuterT = typename ::std::iterator_traits<Iterator>::value_type;
        using T = typename ::std::iterator_traits<typename OuterT::iterator>::value_type;
        ::tools::group<T> group;
        ::std::vector<::std::vector<T>> preprocessed;

      public:
        group2d(const ::tools::group<T>& group, const Iterator& begin, const Iterator& end) :
          group(group),
          preprocessed(
            ::std::distance(begin, end) + 1,
            ::std::vector<T>((begin == end ? 0 : ::std::distance(::std::begin(*begin), ::std::end(*begin))) + 1, group.identity())
          )
        {
          const ::std::size_t height = ::std::distance(begin, end);
          const ::std::size_t width = begin == end ? 0 : ::std::distance(::std::begin(*begin), ::std::end(*begin));

          {
            Iterator it1 = begin;
            for (::std::size_t y = 0; y < height; ++y, ++it1) {
              auto it2 = ::std::begin(*it1);
              for (::std::size_t x = 0; x < width; ++x, ++it2) {
                this->preprocessed[y + 1][x + 1] = group.operation()(this->preprocessed[y + 1][x], *it2);
              }
            }
          }
          for (::std::size_t x = 0; x < width; ++x) {
            for (::std::size_t y = 0; y < height; ++y) {
              this->preprocessed[y + 1][x + 1] = group.operation()(this->preprocessed[y][x + 1], this->preprocessed[y + 1][x + 1]);
            }
          }
        }

        T operator()(const ::std::size_t x1, const ::std::size_t y1, const ::std::size_t x2, const ::std::size_t y2) const {
          T tmp = this->group.operation()(this->preprocessed[x2][y2], this->group.inverse()(this->preprocessed[x1][y2]));
          tmp = this->group.operation()(tmp, this->group.inverse()(this->preprocessed[x2][y1]));
          return this->group.operation()(tmp, this->preprocessed[x1][y1]);
        }
      };
    }
  }

  template <typename Iterator>
  ::std::function<typename ::std::iterator_traits<Iterator>::value_type(::std::size_t, ::std::size_t)> cumsum1d(
    const ::tools::monoid<typename ::std::iterator_traits<Iterator>::value_type>& monoid,
    const Iterator& begin,
    const Iterator& end
  ) {
    return ::tools::detail::cumsum::monoid1d<Iterator>(monoid, begin, end);
  }

  template <typename Iterator>
  ::std::function<typename ::std::iterator_traits<Iterator>::value_type(::std::size_t, ::std::size_t)> cumsum1d(
    const ::tools::group<typename ::std::iterator_traits<Iterator>::value_type>& group,
    const Iterator& begin,
    const Iterator& end
  ) {
    return ::tools::detail::cumsum::group1d<Iterator>(group, begin, end);
  }

  template <typename Iterator>
  ::std::function<typename ::std::iterator_traits<Iterator>::value_type(::std::size_t, ::std::size_t, ::std::size_t, ::std::size_t)> cumsum2d(
    const ::tools::group<typename ::std::iterator_traits<Iterator>::value_type>& group,
    const Iterator& begin,
    const Iterator& end
  ) {
    return ::tools::detail::cumsum::group2d<Iterator>(group, begin, end);
  }
}

#endif

// main.cpp

int main() {
  i64 N;
  std::cin >> N;
  std::vector<i64> A;
  tools::read(A, N);

  const auto cumsum = tools::cumsum1d(tools::group<i64>::plus(), A.begin(), A.end());
  i64 answer = std::numeric_limits<i64>::max();
  for (i64 i = 1; i <= N - 1; ++i) {
    answer = std::min(answer, std::abs(cumsum(0, i) - cumsum(i, N)));
  }
  std::cout << answer << std::endl;
  return 0;
}

Measured memory_bytes: 3956