#include <iostream> #include <cstdint> #include <vector> #include <stack> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int32_t N, i; cin >> N; vector<int32_t> H(N); for (i = 0; i != N; ++i) cin >> H[i]; stack<int32_t> s; int32_t ans = 0, lower; for (i = 0; i != N; ++i) { lower = 0; if (i & 1) while (true) { if (H[i] < s.top()) { ans -= H[i] - lower, s.push(H[i]); break; } ans -= s.top(), s.pop(); if (s.empty() || H[i] <= s.top()) break; else lower = s.top(), s.pop(); } else while (true) { if (s.empty() || H[i] < s.top()) { ans += H[i] - lower, s.push(H[i]); break; } ans += s.top(), s.pop(); if (H[i] <= s.top()) break; else lower = s.top(), s.pop(); } cout << ans << '\n'; } return 0; }