#include #include #include #include #include #include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; #define rep(i, n) for (int i = 0; i< (int)(n); i++) int op(int a, int b){ return a+b; } int e(){return 0;} int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin >> n; deque> dq; vector sum(2, 0); rep(i, n){ ll h; cin >> h; int c = (i%2 == 0 ? 1 : 0); if(dq.size() == 0 && c == 1){ dq.push_back({h, c}); sum[c] += h; }else{ while(!dq.empty() && dq.back().first <= h){ sum[dq.back().second] -= dq.back().first; dq.pop_back(); }if(dq.size() == 0){ if(c == 1){ dq.push_back({h, c}); sum[c] += h; } }else if(dq.back().second != c){ dq.push_back({h, c}); sum[c] += h; } } cout << sum[1] - sum[0] << endl; } return 0; }