#include<bits/stdc++.h> using namespace std; using ll = long long; #define all(a) begin(a), end(a) #define rep(i,a,b) for(ll i = a; i < (b); i++) int main(){ int n; cin >> n; stack<pair<int,int>> st; ll res = 0; rep(i,0,n){ ll h; cin >> h; if(i % 2 == 0){ while(st.size() && st.top().second <= h){ res -= st.top().second - st.top().first; st.pop(); } if(st.size() && st.top().first < h){ res -= h - st.top().first; st.top().first = 0; res += h; }else{ st.push({0,h}); res += h; } }else{ while(st.size() && st.top().second <= h){ res -= st.top().second - st.top().first; st.pop(); } if(st.size() && st.top().first < h){ res -= h - st.top().first; st.top().first = h; } } cout << res << endl; } }