#include using namespace std; int main() { int n = 0; cin >> n; vector h(n,0); for (int i = 0;i < n;i++) { cin >> h[i]; } auto cmp = [](vector a,vector b) -> bool { return a[1] > b[1]; }; priority_queue,vector>,decltype(cmp)> p(cmp); int ans = 0; for (int i = 0;i < n;i++) { if (i % 2 == 0) { while (p.size() != 0 && (p.top())[1] <= h[i]) { ans -= (p.top())[1] - (p.top())[0]; p.pop(); } if (p.size() == 0 || h[i] <= (p.top())[0]) { ans += h[i]; p.push({0,h[i]}); } else { vector temp = p.top(); ans -= temp[1] - temp[0]; p.pop(); ans += temp[1]; p.push({0,temp[1]}); } } else { while (p.size() != 0 && (p.top())[1] <= h[i]) { ans -= (p.top())[1] - (p.top())[0]; p.pop(); } if (p.size() != 0 && (p.top())[0] <= h[i]) { vector temp = p.top(); p.pop(); ans -= temp[1] - temp[0]; temp[0] = h[i]; ans += temp[1] - temp[0]; p.push(temp); } } cout << ans << endl; } }