#include #include #include using namespace std; using ll = long long; struct x{ int h; bool c; }; int main(){ int n; cin >> n; stack st; int ans=0; for(int i=0;i> h.h; while(!st.empty() && st.top().h <=h.h){ x x =st.top(); st.pop(); if(x.c){ ans-=(x.h); } else{ ans+=(x.h); } } if(i%2==0){ if(st.empty() || !st.top().c){ st.push({h.h,true}); ans += h.h; } }else{ if(!st.empty() && st.top().c){ st.push({h.h,false}); ans -= h.h; } } cout << ans << endl; } return 0; }