#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; segs.insert({{0, 1e15}, green}); ll ans = 0; ll i = -1; for(const auto &e:h) { i++; vector dels; for(const auto &f:segs) { dels.push_back(f); if(f.first.second > e) { auto v = f; v.first.first = e; segs.insert(v); if(f.second == cyan) ans += f.first.second - e; break; } } for(const auto &e:dels) { if(e.second == cyan) ans -= e.first.second - e.first.first; segs.erase(e); } segs.insert({{0, e}, i % 2}); if(i % 2 == cyan) ans += e; cout << ans << "\n"; } }