#include <iostream> #include <algorithm> #include <atcoder/all> #include <cassert> using namespace std; using namespace atcoder; using ll = long long; const long long INF = 1000000000000000000; int main(){ ll N; cin >> N; assert(1 <= N && N <= 200000); vector<pair<ll, ll>> v; v.push_back({-1, INF}); ll ans = 0; for(int i = 0; i < N; i++){ ll H; cin >> H; assert(1 <= H && H <= 1e9); while(1){ auto [col, h] = v[(int)v.size() - 1]; if(h <= H){ ans -= col * h; v.pop_back(); }else{ break; } } if(i % 2 == 0){ if(v[(int)v.size() - 1].first != 1){ ans += H; v.push_back({1, H}); } }else{ if(v[(int)v.size() - 1].first != -1){ ans += -H; v.push_back({-1, H}); } } cout << ans << endl; } return 0; }