#include #include //小数点出力用 //cout << fixed << setprecision(10) << ans; #include #include #include #include #include #include #include using ll = long long; using namespace std; #define modP 998244353 bool chkrng0idx(int pos, int sup) { return (0 <= pos && pos < sup); } int clk4(int num) { return (num - 2) * (num % 2); } void yn(bool tf) { cout << (tf ? "Yes\n" : "No\n"); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N;cin >> N; stack>>ST; ST.push({ (ll)1e10,{0,(ll)1e10} }); ll ans = 0; for (int i = 0;i < N;i++) { ll H;cin >> H; while (ST.top().first <= H) { if (ST.top().second.first == 1) { ans -= ST.top().second.second; } ST.pop(); } if (ST.top().second.first == i % 2) { if(ST.top().second.first==1)ans -= H - (ST.top().first - ST.top().second.second); ST.top().second.second = ST.top().first - H; ST.push({ H,{1 - i % 2, H} }); if (i % 2 == 0)ans += H; } else { if (ST.top().second.first == 1)ans += ST.top().first - ST.top().second.second; ST.top().second.second = ST.top().first; } cout << ans << endl; } return 0; }