#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector dp(N); for (int i = N - 1; i >= 0; i--) { if (A[i] == 1) { if (i == N - 1) { ; } else if (i == N - 2) { dp[i] = 1; } else { if (A[i + 1] == 0) { dp[i] = (dp[i + 2] + 2); } else { dp[i] = 1; } } } } long long ans = 0; for (int i = 0; i < N; i++) { ans += dp[i]; } cout << ans << endl; return 0; }