#include using namespace std; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fast_io(); int n; cin >> n; const int INF = 2e9; vector st{INF}; int cur = 0; for (int i = 0; i < n; i++) { int h; cin >> h; while (!st.empty() && st.back() <= h) { if (st.size() % 2) { cur += st.back(); } else { cur -= st.back(); } st.pop_back(); } if (st.size() % 2 != i % 2) { st.push_back(h); if (i % 2 == 0) { cur += h; } else { cur -= h; } } cout << cur << endl; } }