#include using namespace std; struct onsen { int l, r, c; bool operator<(const onsen &other) const { if (r != other.r) return r < other.r; if (l != other.l) return l < other.l; return c < other.c; } bool operator==(const onsen &other) const { return l == other.l && r == other.r && c == other.c; } }; int main() { int N; cin >> N; vector H(N); for (int i = 0; i < N; i++) { cin >> H[i]; } set st; int ans = 0; for (int i = 0; i < N; i++) { auto it = st.lower_bound({0, H[i], 0}); if (it == st.end()) { while (!st.empty()) { onsen o = *st.begin(); if (o.c == 0) { ans -= o.r - o.l + 1; } st.erase(st.begin()); } if (i % 2 == 0) { ans += H[i]; } st.insert({1, H[i], i % 2}); } else { if (it->c != i % 2) { onsen o = *it; while (1) { onsen now = *st.begin(); st.erase(st.begin()); if (now.c == 0) { ans -= now.r - now.l + 1; } if (now == o) { break; } } st.insert({o.l, H[i], i % 2}); if (i % 2 == 0) { ans += H[i] - o.l + 1; } st.insert({H[i] + 1, o.r, o.c}); if (o.c == 0) { ans += o.r - (H[i] + 1) + 1; } } else { onsen o = *it; while (1) { onsen now = *st.begin(); st.erase(st.begin()); if (now.c == 0) { ans -= now.r - now.l + 1; } if (now == o) { break; } } st.insert({1, o.r, i % 2}); if (i % 2 == 0) { ans += o.r; } } } cout << ans << "\n"; } }