#include #include #pragma region Header #pragma GCC target("avx2") #pragma GCC optimize("unroll-loops") #pragma region TypeAlias using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using f64 = double; using f80 = long double; using f128 = __float128; constexpr i32 operator"" _i32(u64 v) { return v; } constexpr u32 operator"" _u32(u64 v) { return v; } constexpr i64 operator"" _i64(u64 v) { return v; } constexpr u64 operator"" _u64(u64 v) { return v; } constexpr f64 operator"" _f64(f80 v) { return v; } constexpr f80 operator"" _f80(f80 v) { return v; } using Istream = std::istream; using Ostream = std::ostream; using Str = std::string; template using Lt = std::less; template using Gt = std::greater; template using IList = std::initializer_list; template using BSet = std::bitset; template using Pair = std::pair; template using Tup = std::tuple; template using Arr = std::array; template using Deq = std::deque; template using Set = std::set; template using MSet = std::multiset; template using USet = std::unordered_set; template using UMSet = std::unordered_multiset; template using Map = std::map; template using MMap = std::multimap; template using UMap = std::unordered_map; template using UMMap = std::unordered_multimap; template using Vec = std::vector; template using Stack = std::stack; template using Que = std::queue; template using MaxHeap = std::priority_queue; template using MinHeap = std::priority_queue, Gt>; #pragma endregion #pragma region Constants template constexpr T INF = std::numeric_limits::max() / 4; template constexpr T PI = T{3.141592653589793238462643383279502884}; template constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN(n - 1) * T{10}; } #pragma endregion #pragma region FuncAlias template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else { return false; } } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else { return false; } } template T fdiv(T x, T y) { if (y < T{}) { x = -x, y = -y; } return x >= T{} ? x / y : (x - y + 1) / y; } template T cdiv(T x, T y) { if (y < T{}) { x = -x, y = -y; } return x >= T{} ? (x + y - 1) / y : x / y; } template T power(T v, I n) { T ans = 1; for (; n > 0; n >>= 1, v *= v) { if (n % 2 == 1) { ans *= v; } } return ans; } template T power(T v, I n, const T& e) { T ans = e; for (; n > 0; n >>= 1, v *= v) { if (n % 2 == 1) { ans *= v; } } return ans; } template void fillAll(Vec& vs, const T& v) { std::fill(vs.begin(), vs.end(), v); } template> void sortAll(Vec& vs, C comp = C{}) { std::sort(vs.begin(), vs.end(), comp); } template void reverseAll(Vec& vs) { std::reverse(vs.begin(), vs.end()); } template void uniqueAll(Vec& vs) { sortAll(vs); vs.erase(std::unique(vs.begin(), vs.end()), vs.end()); } template void iotaAll(Vec& vs, T offset = T{}) { std::iota(vs.begin(), vs.end(), offset); } template V sumAll(const Vec& vs) { return std::accumulate(vs.begin(), vs.end(), V{}); } template int minInd(const Vec& vs) { return std::min_element(vs.begin(), vs.end()) - vs.begin(); } template int maxInd(const Vec& vs) { return std::max_element(vs.begin(), vs.end()) - vs.begin(); } template int lbInd(const Vec& vs, const T& v) { return std::lower_bound(vs.begin(), vs.end(), v) - vs.begin(); } template int ubInd(const Vec& vs, const T& v) { return std::lower_bound(vs.begin(), vs.end(), v) - vs.begin(); } template Vec genVec(int n, F gen) { Vec ans; std::generate_n(std::back_insert_iterator(ans), n, gen); return ans; } template Vec iotaVec(int n, T offset = T{}) { Vec ans(n); iotaAll(ans, offset); return ans; } template> Vec iotaVec(const Vec& vs, F comp = F{}) { auto is = iotaVec(vs.size(), 0); sortAll(is, [&](int i, int j) { return comp(vs[i], vs[j]); }); return is; } template Vec operator+=(Vec& vs1, const Vec& vs2) { vs1.insert(vs1.end(), vs2.begin(), vs2.end()); return vs1; } template Vec operator+(const Vec& vs1, const Vec& vs2) { return Vec{vs1} += vs2; } #pragma endregion #pragma region Show #pragma endregion #pragma region BitOps constexpr int popcount(const u64 v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const u64 v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const u64 v) { return __builtin_ffsll(v); } constexpr int clog(const u64 v) { return v ? log2p1(v - 1) : 0; } constexpr u64 ceil2(const u64 v) { return 1_u64 << clog(v); } constexpr u64 floor2(const u64 v) { return v ? (1_u64 << (log2p1(v) - 1)) : 0_u64; } constexpr bool ispow2(const u64 v) { return (v & (v - 1)) == 0; } constexpr bool btest(const u64 mask, const int ind) { return (mask >> ind) & 1_u64; } #pragma endregion #pragma region FixPoint template struct Fixpoint : F { Fixpoint(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; #pragma endregion #pragma region NdVec template auto ndVec(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], ndVec(szs, x)); } } #pragma endregion #pragma region Range class range { private: struct itr { itr(int start = 0, int step = 1) : m_cnt{start}, m_step{step} {} bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; } int operator*() { return m_cnt; } itr& operator++() { m_cnt += m_step; return *this; } int m_cnt, m_step; }; int m_start, m_end, m_step; public: range(int start, int end, int step = 1) : m_start{start}, m_end{end}, m_step{step} { assert(m_step == 1 or m_step == -1); } itr begin() const { return itr{m_start, m_step}; } itr end() const { return itr{m_end, m_step}; } }; range rep(int end) { return range(0, end, 1); } range per(int rend) { return range(rend - 1, -1, -1); } class ndRep { private: struct itr { itr(const Vec& ns) : m_ns{ns}, m_cs(ns.size(), 0), m_end{false} {} bool operator!=(const itr&) const { return not m_end; } const Vec& operator*() { return m_cs; } itr& operator++() { for (const int i : per(m_ns.size())) { m_cs[i]++; if (m_cs[i] < m_ns[i]) { break; } else { if (i == 0) { m_end = true; } m_cs[i] = 0; } } return *this; } Vec m_ns, m_cs; bool m_end; }; Vec m_ns; public: ndRep(const Vec& ns) : m_ns{ns} {} itr begin() const { return itr{m_ns}; } itr end() const { return itr{m_ns}; } }; #pragma endregion #pragma message("[REFS] Xoshiro: https://prng.di.unimi.it") #pragma region Xoshiro namespace xoshiro_impl { u64 x; u64 next() { uint64_t z = (x += 0x9e3779b97f4a7c15); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } } class Xoshiro32 { public: using result_type = u32; using T = result_type; Xoshiro32(T seed = 0) { xoshiro_impl::x = seed; s[0] = xoshiro_impl::next(); s[1] = xoshiro_impl::next(); s[2] = xoshiro_impl::next(); s[3] = xoshiro_impl::next(); } static constexpr T min() { return std::numeric_limits::min(); } static constexpr T max() { return std::numeric_limits::max(); } T operator()() { return next(); } private: static constexpr T rotl(const T x, int k) { return (x << k) | (x >> (32 - k)); } T next() { const T ans = rotl(s[1] * 5, 7) * 9; const T t = s[1] << 9; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 11); return ans; } T s[4]; }; class Xoshiro64 { public: using result_type = u64; using T = result_type; Xoshiro64(T seed = 0) { xoshiro_impl::x = seed; s[0] = xoshiro_impl::next(); s[1] = xoshiro_impl::next(); s[2] = xoshiro_impl::next(); s[3] = xoshiro_impl::next(); } static constexpr T min() { return std::numeric_limits::min(); } static constexpr T max() { return std::numeric_limits::max(); } T operator()() { return next(); } private: static constexpr T rotl(const T x, int k) { return (x << k) | (x >> (64 - k)); } T next() { const T ans = rotl(s[1] * 5, 7) * 9; const T t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 45); return ans; } T s[4]; }; #pragma endregion #pragma region RNG template class RNG { public: using result_type = typename Rng::result_type; using T = result_type; static constexpr T min() { return Rng::min(); } static constexpr T max() { return Rng::max(); } RNG() : RNG(std::random_device{}()) {} RNG(T seed) : m_rng(seed) {} T operator()() { return m_rng(); } template T val(T min, T max) { return std::uniform_int_distribution(min, max)(m_rng); } template Pair pair(T min, T max) { return std::minmax({val(min, max), val(min, max)}); } template Vec vec(int n, T min, T max) { return genVec(n, [&]() { return val(min, max); }); } template Vec> vvec(int n, int m, T min, T max) { return genVec>(n, [&]() { return vec(m, min, max); }); } private: Rng m_rng; }; RNG rng; RNG rng64; RNG rng_xo; RNG rng_xo64; #pragma endregion struct modinfo { void set_mod(const u32 nmod) { mod = nmod; } u32 mod, root, max2p; }; template class modint { public: static constexpr const u32& mod = info.mod; static constexpr const u32& root = info.root; static constexpr const u32& max2p = info.max2p; constexpr modint() : m_val{0} {} constexpr modint(const i64 v) : m_val{normll(v)} {} constexpr void set_raw(const u32 v) { m_val = v; } constexpr modint operator-() const { return modint{0} - (*this); } constexpr modint& operator+=(const modint& m) { return m_val = norm(m_val + m()), *this; } constexpr modint& operator-=(const modint& m) { return m_val = norm(m_val + mod - m()), *this; } constexpr modint& operator*=(const modint& m) { return m_val = normll((i64)m_val * (i64)m() % (i64)mod), *this; } constexpr modint& operator/=(const modint& m) { return *this *= m.inv(); } constexpr modint operator+(const modint& m) const { return modint{*this} += m; } constexpr modint operator-(const modint& m) const { return modint{*this} -= m; } constexpr modint operator*(const modint& m) const { return modint{*this} *= m; } constexpr modint operator/(const modint& m) const { return modint{*this} /= m; } constexpr bool operator==(const modint& m) const { return m_val == m(); } constexpr bool operator!=(const modint& m) const { return not(*this == m); } friend std::istream& operator>>(std::istream& is, modint& m) { i64 v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint& m) { return os << m(); } constexpr u32 get() const { return m_val; } constexpr u32 operator()() const { return m_val; } template constexpr modint pow(Int n) const { return power(*this, n); } constexpr modint inv() const { return pow(mod - 2); } static modint sinv(const u32 n) { static std::vector is{1, 1}; for (u32 i = (u32)is.size(); i <= n; i++) { is.push_back(-is[mod % i] * (mod / i)); } return is[n]; } static modint fact(const u32 n) { static std::vector fs{1, 1}; for (u32 i = (u32)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); } return fs[n]; } static modint ifact(const u32 n) { static std::vector ifs{1, 1}; for (u32 i = (u32)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); } return ifs[n]; } static modint comb(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k); } private: static constexpr u32 norm(const u32 x) { return x < mod ? x : x - mod; } static constexpr u32 normll(const i64 x) { return norm(u32(x % (i64)mod + (i64)mod)); } u32 m_val; }; constexpr modinfo modinfo_1000000007 = {1000000007, 5, 1}; constexpr modinfo modinfo_998244353 = {998244353, 3, 23}; using modint_1000000007 = modint; using modint_998244353 = modint; #pragma region Printer class printer { public: printer(Ostream& os = std::cout) : m_os{os} { m_os << std::fixed << std::setprecision(15); } template int operator()(const Args&... args) { dump(args...); return 0; } template int ln(const Args&... args) { dump(args...), m_os << '\n'; return 0; } template int el(const Args&... args) { dump(args...), m_os << std::endl; return 0; } private: template void dump(const T& v) { m_os << v; } template void dump(const Vec& vs) { for (const int i : rep(vs.size())) { m_os << (i ? " " : ""), dump(vs[i]); } } template void dump(const Vec>& vss) { for (const int i : rep(vss.size())) { m_os << (i ? "" : "\n"), dump(vss[i]); } } template int dump(const T& v, const Ts&... args) { dump(v), m_os << ' ', dump(args...); return 0; } Ostream& m_os; }; printer out; #pragma endregion #pragma region Scanner class scanner { public: scanner(Istream& is = std::cin) : m_is{is} { m_is.tie(nullptr)->sync_with_stdio(false); } template T val() { T v; return m_is >> v, v; } template T val(T offset) { return val() - offset; } template Vec vec(int n) { return genVec(n, [&]() { return val(); }); } template Vec vec(int n, T offset) { return genVec(n, [&]() { return val(offset); }); } template Vec> vvec(int n, int m) { return genVec>(n, [&]() { return vec(m); }); } template Vec> vvec(int n, int m, const T offset) { return genVec>(n, [&]() { return vec(m, offset); }); } template auto tup() { return Tup{val()...}; } template auto tup(const Args&... offsets) { return Tup{val(offsets)...}; } private: Istream& m_is; }; scanner in; #pragma endregion #pragma endregion int main() { const auto N = in.val(); const auto as = in.vec(N); Vec os; int l = 0; for (const int i : rep(N)) { if (as[i] == 1) { l++; } else { if (l) { os.push_back(l); } l = 0; } } if (l) { os.push_back(l); } void(0); i64 ans = (i64)N * (N + 1) / 2; for (const i64 o : os) { ans -= o * (o + 1) / 2; }; out.ln(ans); return 0; }