#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using pall = pair; using vell = vector; template constexpr bool mini(T &var, const U &val) noexcept { const bool cmp = var > val; if(cmp) var = val; return cmp; } template constexpr bool maxi(T &var, const U &val) noexcept { const bool cmp = var < val; if(cmp) var = val; return cmp; } void solve(); int main(void) { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; } enum color { cyan = 0, green = 1 }; void solve() { ll n; cin >> n; vell h(n); for(auto &e:h) cin >> e; set segs; ll ans = 0; ll i = -1; for(const auto &e:h) { i++; vector dels; for(const auto &f:segs) { if(f.first < e) { dels.push_back(f); if(f.second > e) { auto v = f; v.first = e; segs.insert(v); ans += v.second - v.first; break; } } else break; } for(const auto &e:dels) { ans -= e.second - e.first; segs.erase(e); } if(i % 2 == cyan) { ans += e; segs.insert({0, e}); } cout << ans << "\n"; } }