#include using namespace std; int main(){ int N; cin >> N; vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } vector dp(N, 0); 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 { dp[i] += 2; if (i < N - 2){ dp[i] += dp[i + 2]; } } } long long ans = 0; for (int i = 0; i < N; i++){ ans += dp[i]; } cout << ans << endl; }