#include #define rep(i, n) for (int i = 0; i < (n); ++i) using std::cin; using std::cout; using std::vector; using ll = long long; int main() { int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; vector dp(n); for (int i = n-2; i >= 0; --i) { if (a[i] == 0) dp[i] = 0; else if (a[i+1] == 1) dp[i] = 1; else { if (i == n-2) dp[i] = 1; else dp[i] = dp[i+2] + 2; } } ll ans = 0; rep(i, n) ans += dp[i]; cout << ans << '\n'; return 0; }