#include using namespace std; int main () { int n; cin >> n; stack state; state.push(1e9 + 10); int height = 0; for(int i = 0; i < n; i++){ int h; cin >> h; while(true){ int last = state.top(); if(last > h) break; state.pop(); if(state.size() % 2 == 0){ height += last; } else { height -= last; } } if((state.size() - i) % 2 == 1){ if(i % 2 == 0){ state.push(h); height += h; } else { state.push(h); height -= h; } } cout << height << endl; } }